Spring Boot - Caching
You want to cache results of a method that returns a list of users filtered by role. How do you ensure caching works correctly for different roles?
To cache results differently based on the role parameter, specify a key using SpEL expression referencing the role.
Use @Cacheable(cacheNames = "users", key = "#role") to cache by role parameter. correctly uses key = "#role" to cache by role. Use @Cacheable(cacheNames = "users") without key; caching will auto-differentiate. caches all calls under same key, causing wrong reuse. Use @Cacheable(cacheNames = "users", key = "#root.methodName") to cache by method name. caches by method name only, ignoring parameters. Caching cannot be used with methods returning lists. is false.
15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions