Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.1 Cache Abstraction without parameters

Tags:

caching

spring

reading about the new Cache Abstraction in Spring 3.1, I wanted to apply this feature to my project.

Can I cache the call to a method that has no parameters?

@Cacheable("xCache")
public List<X> loadAllX() {
    ...
}

The linked blog post states

a cache lookup is performed using as key the method parameters

so it should not be possible to cache this method, right?

Short answer: Yes, methods without any arguments will get cached just like any other methods. I guess there will be exactly one entry in the cache for that method.

like image 555
Steven Avatar asked May 16 '12 05:05

Steven


1 Answers

You can override this behavior by using "Cache SpEL available metadata" as described here:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/cache.html#cache-spel-context

In your example, you could specify the following:

@Cacheable(value = "xCache", key = "#root.methodName") 
public List<X> loadAllX() { 
    ... 
} 

Which would cache the list of X in the "xCache" with key "loadAllX"

like image 125
btiernay Avatar answered Nov 09 '22 08:11

btiernay