Reputation: 55032
I can do the following while using Cacheable:
@Cacheable(value = CacheUtil.LEVEL1, key = "'Location'+#root.methodName+'_'+#country+'_'+#lang")
public List<Location> getCities(String country, String lang)
and it works fine.
I am not sure about the following though. How can I cache the following Method with Cacheable?
public Content getContent(ContentRequest Request)
How should I write the @Cacheable so that It works?
Thanks.
Upvotes: 2
Views: 2784
Reputation: 7950
You need something like this:
@Cacheable(value = CacheUtil.LEVEL1, keyGenerator="contentRequestKeyGenerator")
public Content getContent(ContentRequest Request)
Where contentRequestKeyGenerator is the name of a bean that implements the KeyGenerator interface see http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/interceptor/KeyGenerator.html
In the past I have had one bean that does key generation for several different cache classes, in the example below objects[0] is the class of the calling method, ContentRequest in your example :
@Component
public class MyKeyGenerator implements KeyGenerator{
@Override
public Object generate(Object o, Method method, Object... objects) {
if (String.class.isInstance(objects[0])) {
return ....
}
else if (....) {
}
}
You would then use this :
@Cacheable(value = "properties", keyGenerator = "myKeyGenerator")
public Property getProperty(String key) {
Do you need to do this? The default is the hashCode of ContentRequest, usually this is good enough.
Upvotes: 2