I've looked everywhere online for a simple answer to the above question, and just can't find it. I have a method like:
@Cacheable(cacheNames = "objects")
public List<Object> get() { .. }
I'm using EhCache 2.10.0 with Spring Framework 4.2.1, and I can see the following output for this method:
Adding cacheable method 'get' with attribute: [CacheableOperation[public java.util.List com.example.DAO.get()] caches=[objects] | key='' ...
also (in later logging)
Computed cache key 'SimpleKey []' for operation ...
What is the @Cacheable annotation supposed to do in this situation? Place each object in the cache keyed by hashCode (as hinted at elsewhere)? Or just plonk the whole list as-is into the cache under some list-based hashCode?
The Spring Doc mentions this:
Since caches are essentially key-value stores, each invocation of a cached method needs to be translated into a suitable key for cache access. Out of the box, the caching abstraction uses a simple KeyGenerator based on the following algorithm:
If no params are given, return
SimpleKey.EMPTY
If only one param is given, return that instance
If more the one param is given, return a key computed from the hashes of all parameters.
https://docs.spring.io/spring/docs/5.0.8.RELEASE/spring-framework-reference/integration.html#cache
When you annotate it with @Cacheable(cacheNames = "objects")
you give it a cache name against which it can identify the cache. Since your method doesn't have any arguments, for the cacheName as "objects", it will have already cached the return object (List<Object>
in this case). and every time the method is called, it will check the cache with the cacheName object and with the key value as '0'. If it already has a return object that was cached previous time, it will return the object.
Updated: with SimpleKey.EMPTY
key for no params for Spring 4+