Spring 3.1 cache - how to use return value in SpEL

I am trying to do an eviction of a record in a spring-driven cache (Spring 3.1 abstraction).

I need to reference the return value of the method in the SpEL of the "key" property in the annotation:

    /* (How to refer to the 'T' returned value in the "KEY_ID"?) */
@Caching(evict = { @CacheEvict(value = CACHE_BY_ID, key = KEY_ID) })
public T delete(AppID appID, UserID userID) throws UserNotFoundException {
    return inner.delete(appID, userID);
}

Is there any way to do this?

+3
source share
1 answer

It doesn't seem like you can reference the returned object:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/cache.html#cache-spel-context

But why do you need this? You can refer to arguments in the key value @CacheEvict, for example:

@CacheEvict(value = CACHE_BY_ID, key = "#userID")
public T delete(AppID appID, UserID userID) throws UserNotFoundException {
...
}

More sample code in response to the answer below about the need to evict from several caches using several properties of the User object:

@Caching(evict = {
    @CacheEvict(value = CACHE_BY_ID, key = "#user.userID"),
    @CacheEvict(value = CACHE_BY_LOGIN_NAME, key = "#user.loginName")
    // etc.
})
public T delete(AppID appID, User user) throws UserNotFoundException {
...
}
+2

All Articles