Bird
0
0

You want to cache results of a method that takes two parameters: userId (Long) and filter (String). Which cache key strategy ensures unique keys for each combination?

hard📝 Application Q15 of 15
Spring Boot - Caching
You want to cache results of a method that takes two parameters: userId (Long) and filter (String). Which cache key strategy ensures unique keys for each combination?
AUse <code>key = "T(java.util.Objects).hash(#userId, #filter)"</code>
BUse <code>key = "#userId + #filter"</code> without conversion
CUse <code>key = "#userId"</code> only
DUse <code>key = "#filter"</code> only
Step-by-Step Solution
Solution:
  1. Step 1: Understand need for unique keys per parameter combo

    Using only one parameter as key risks collisions when other parameter changes.
  2. Step 2: Evaluate options for combining parameters

    Use key = "#userId + #filter" without explicit conversion risks type issues. Use key = "T(java.util.Objects).hash(#userId, #filter)" uses Java's Objects.hash to combine both safely. Using only one parameter causes collisions.
  3. Final Answer:

    Use key = "T(java.util.Objects).hash(#userId, #filter)" -> Option A
  4. Quick Check:

    Combine keys safely with Objects.hash [OK]
Quick Trick: Use Objects.hash to combine multiple parameters safely [OK]
Common Mistakes:
  • Concatenating keys without type safety
  • Using only one parameter as key causing collisions
  • Ignoring proper SpEL syntax for static method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes