0
0
Spring Bootframework~10 mins

Cache key strategies in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify a cache key using Spring's @Cacheable annotation.

Spring Boot
@Cacheable(value = "users", key = "#[1]")
public User getUserById(Long id) {
    // method body
}
Drag options to blanks, or click blank then click option'
Avalue
BuserId
Ckey
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not exist in the method signature.
Omitting the # prefix in the key expression.
2fill in blank
medium

Complete the code to create a cache key based on multiple method parameters.

Spring Boot
@Cacheable(value = "products", key = "#[1] + '-' + #[2]")
public Product getProduct(String category, Long productId) {
    // method body
}
Drag options to blanks, or click blank then click option'
Acategory
BproductId
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names that don't match the method signature.
Not concatenating parameters properly in the key expression.
3fill in blank
hard

Fix the error in the cache key expression to correctly use the method parameter.

Spring Boot
@Cacheable(value = "orders", key = "#[1]")
public Order findOrder(Long orderId) {
    // method body
}
Drag options to blanks, or click blank then click option'
Aid
BorderId
Corder
DorderID
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name with incorrect case.
Using a parameter name that does not exist.
4fill in blank
hard

Fill both blanks to create a cache key using two parameters separated by an underscore.

Spring Boot
@Cacheable(value = "sessions", key = "#[1] + '_' + #[2]")
public Session getSession(String userId, String sessionId) {
    // method body
}
Drag options to blanks, or click blank then click option'
AuserId
BsessionId
Cid
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names.
Forgetting to add quotes around the underscore.
5fill in blank
hard

Fill all three blanks to create a cache key using a method parameter and a method call on it.

Spring Boot
@Cacheable(value = "employees", key = "#[1] + '-' + #[2].[3]()")
public Employee getEmployee(EmployeeId empId) {
    // method body
}
Drag options to blanks, or click blank then click option'
AempId
CtoString
DgetId
Attempts:
3 left
💡 Hint
Common Mistakes
Using different parameter names in blanks 1 and 2.
Using a method name that does not exist on the parameter.