call a static method on a parameter while using @CacheResult

774 views Asked by At

I need to cache the result of a method that call a webservice. The method signature is like this :

public Result search(long id, String name, Date date);

and the result depends on all the parameters

I created the ehcache configuration for the cache myCache, normally to use this cache I should use the @CacheResult annotation :

@CacheResult(cacheName = "myCache")
public Result search(long id, String name, Date date);

But in my case I need to call a static method on the date parameter, I want to do it the same way as the @Cacheable annotation :

@Cacheable(value = "myCache", key ="{#id, #name, T(my.static).method(#date)}")
public Result search(long id, String name, Date date);

My question is how could I call a static method on a parameter while using @CacheResult ?

1

There are 1 answers

0
Vassilis Bekiaris On

@CacheResult supplies a way to customize the generated key by defining a key generator class like this:

@CacheResult(cacheKeyGenerator = CustomKeyGenerator.class)
public Result search(long id, String name, Date date); 

It does not support definition of key generation in terms of SpEL evaluation directly in the annotation; instead you must provide your own implementation of javax.cache.annotation.CacheKeyGenerator:

public class CustomKeyGenerator implements CacheKeyGenerator {

    @Override
    public GeneratedCacheKey generateCacheKey(CacheKeyInvocationContext<? extends Annotation> cacheKeyInvocationContext) {
        CacheInvocationParameter[] parameters = cacheKeyInvocationContext.getKeyParameters();
        // calculate a key based on parameters
        return new SearchKey();
    }