Reputation: 1135
If I am defining a ehcache for a method which doesn't have any parameter.
But in my use case I needs to access the my built cache through it's key.
So please provide me the better way of assigning key on it.
Follow is my code:
@Override
@Cacheable(value = "cacheName", key = "cacheKey")
public List<String> getCacheMethod() throws Exception{
P.S. I am getting the following error when I am trying to access this method from somewhere else.
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'cacheKey' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'
Upvotes: 9
Views: 33347
Reputation: 3124
You can solve this using a single quote in the key that makes it String again.
@Cacheable(value= CACHE_NAME.PRODUCT_CATLOG, key=" 'products' ")
public List<Product> findAll() {
return StreamSupport.stream(productRepository.findAll().spliterator(),false).collect(Collectors.toList());
}
Upvotes: 4
Reputation: 7521
In simple cases you can use an easier way: @Cacheable(key = "#root.methodName")
, and the key would be equal to annotated method's name
Upvotes: 7
Reputation: 16291
The method has no parameter, therefore there is no a way to use a parameter/argument as a default key, and you can't use "static text" as a Key, you could do the following:
Declare the following
public static final String KEY = "cacheKey";
public
static
and final
Then
@Override
@Cacheable(value = "cacheName", key = "#root.target.KEY")
public List<String> getCacheMethod() throws Exception{
Done
Upvotes: 24
Reputation: 4158
Please take a look at this spring doc.
the key refers to the arguments of your method, you are having SpelEvaluationException
because cachekey
isn't among your method arguments.
Upvotes: 2