What is the best way to define a key for @Cacheable annotation for Spring
If I define ehcache for a method that has no parameter.
But in my case, I have to access the inline cache via its key.
So please provide me with a better way to assign a key to it.
Follow my code:
@Override
@Cacheable(value = "cacheName", key = "cacheKey")
public List<String> getCacheMethod() throws Exception{
PS I am getting the following error when I try to access this method from somewhere else.
org.springframework.expression.spel.SpelEvaluationException: EL1008E: (pos 0): Field or property "cacheKey" could not be found on object of type "org.springframework.cache.interceptor.CacheExpressionRootObject"
source to share
The method has no parameter, so there is no way to use the parameter / argument as the default key, and you cannot use "static text" as the key, you can do the following:
Declare the following
public static final String KEY = "cacheKey";
- it should be
public
- should be
static
andfinal
Then
@Override
@Cacheable(value = "cacheName", key = "#root.target.KEY")
public List<String> getCacheMethod() throws Exception{
Done
source to share
Please take a look at this spring doc .
the key refers to the arguments of your method, you have SpelEvaluationException
because it is cachekey
not included in your method arguments.
source to share