9

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'

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
BITSSANDESH
  • 1,025
  • 4
  • 13
  • 23
  • 3
    Well, there are no parameters in your method signature, so even if `key="cacheKey"` *was* a valid SpEL ([it isn't](http://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/html/cache.html#cache-annotations-cacheable-key)) it wouldn't work since there's no such parameter. Hard to tell what you're hoping to achieve since it isn't how `@Cacheable` works - it *does* need method paramaters to use as keys, otherwise the cache will have the capacity of one item. – kryger Aug 02 '14 at 12:52
  • you are absolutely correct with the @Cacheable annotation, when i try to access the cache programmatically it's returning the correct object and also if i am trying to access from same class it's working fine, but when i try to access the method from another class its throw the error "EL1008E:(pos 0): Field or property 'cacheKey' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'" – BITSSANDESH Aug 02 '14 at 13:01
  • It's not clear what you're asking (you seem to have changed the direction of your question three times), sounds like it could be related to THIS problem → http://stackoverflow.com/q/12115996/1240557 ? – kryger Aug 02 '14 at 13:05
  • Kryger you almost get it what i am asking, i have solved the problem of proxies by accessing the cache programmatically Cache cache = cacheManager.getCache("cacheName"); if (cache.get("cacheKey") != null) { return (List) cache.get("cacheKey").get(); } and its work fine in same class but when it comes for accessing the cached method from different class it throws the spring spel exception – BITSSANDESH Aug 02 '14 at 13:11
  • Have you tried removing `key` from the annotation configuration? As per my first comment it's incorrect for more than one reason. However, it's not clear what's the meaning of the `"cacheKey"` string in the snippet you included in your comment above and how you expect it to be related to the `@Cacheable` annotation. Are you *sure* you read the documentation? – kryger Aug 02 '14 at 13:28
  • kryger i think you might be correct, but as can in my previous comment i have to access the cached List from cache object, there i am trying to get the cache object without key then i am getting only null. – BITSSANDESH Aug 02 '14 at 13:33

4 Answers4

24

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";
  • must be public
  • must be static and final

Then

@Override
@Cacheable(value = "cacheName", key = "#root.target.KEY")
public List<String> getCacheMethod() throws Exception{

Done

iamprem
  • 606
  • 2
  • 9
  • 23
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
7

In simple cases you can use an easier way: @Cacheable(key = "#root.methodName"), and the key would be equal to annotated method's name

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
4

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());
    }
Kumar Abhishek
  • 3,004
  • 33
  • 29
2

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.

kryger
  • 12,906
  • 8
  • 44
  • 65
Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38