Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Spring's @Cacheable do when placed on a method returning a list?

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?

like image 918
ben3000 Avatar asked Sep 17 '15 07:09

ben3000


People also ask

How does @cacheable work in spring?

The @Cacheable annotation takes care of putting the result into the cache. After the first call, the cached value is in the cache and stays there according to the cache configuration. When the method is called the second time, and the cache value has not been evicted yet, Spring will search for the value by the key.

What is @cacheable in spring boot?

@CacheableThis method-level annotation lets Spring Boot know that the return value of the annotated method can be cached. Each time a method marked with this @Cacheable is called, the caching behavior will be applied.

How do you cache a method in Java?

Use AOP compile time weaving like this one - it requires changes in the build. Use @Cacheable in Spring - I have to define a cache for each cacheable method.

How does spring boot cache work?

Introduction to Spring Boot Cache. Spring boot provides us strong caching method which can be used to prevent the number of executions of a particular code. By using a cache, we can store the data into a disk or the memory itself, which prevents us from the unnecessary access to a similar type of data in the code.


1 Answers

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+

like image 172
Karthik R Avatar answered Oct 04 '22 14:10

Karthik R