ctw_87
ctw_87

Reputation: 1

Spring cache keys for Collections

In spring cache(ehcache), we can give a key for single parameter or result. As an example:

@CachePut(value="employeeCache", key="#employeeId")
public void addEmployee( int employeeId ){ //body }

@Cacheable(value="employeeCache", key="#result.id")
public Employee getEmployee( int employeeId ){ return employeeObj; }

But How can we define keys for collections like Set, List using annotations?

Upvotes: 0

Views: 3126

Answers (1)

Henri
Henri

Reputation: 5721

By default, the parameters are used for the key. So if you pass a collection in parameter, the collection will be the key. Then it all depends on the cache implementation. In general, it will behave like a map so the equals and hashcode of the collection will be used to find the cache entry. That's how Ehcache works.

If you want to cache each entry of a collection returned by a CachePut method per id... then I don't think it is possible. You will need to do it manually. However, of course, the entire collection can be cached with the defined key.

Upvotes: 1

Related Questions