By default, Spring Boot uses the method parameters to generate a unique cache key using SimpleKeyGenerator. This ensures that different calls with different parameters are cached separately.
@Cacheable(value = "users", key = "#user.id")
public User getUser(User user) { ... }
What will be used as the cache key when this method is called?@Cacheable(value = "users", key = "#user.id") public User getUser(User user) { return user; }
The key attribute uses Spring Expression Language (SpEL). Here, #user.id extracts the id property from the user parameter to use as the cache key.
In SpEL, the plus sign (+) concatenates strings. So #id + '-' + #type creates a string key combining both parameters separated by a dash.
@Cacheable(value = "users", key = "#user") public User getUser(User user) { ... }
When using an object as a cache key, it must properly implement equals() and hashCode(). Otherwise, different instances with same data are treated as different keys, causing cache misses.
@Cacheable(value = "orders", key = "#order.customer.id") public Order getOrder(Order order) { ... }
The key expression uses only the customer id, so different Order objects with the same customer id will map to the same cache key, sharing the cached result.