2012-05-21 26 views
3

我试图逐出Spring管理的缓存(Spring 3.1抽象)中的条目。Spring 3.1缓存 - 如何使用SpEL中的返回值

我需要参考该方法的返回值在规划环境地政司的“钥匙”属性中的注释:

/* (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); 
} 

有没有办法做到这一点?

+0

在http://www.infoq.com/presentations/Spring-Security-3(at 1:04:00),Wiesner使用filterObject作为返回值的句柄,但这可能只是一个Spring Security句柄。值得一试我猜。 –

+1

你还不能看到[SPR-8871](https://jira.springsource.org/browse/SPR-8871)并参与其中。 –

回答

2

它似乎并不像有什么办法可以参考返回的对象:

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

但是,为什么你需要做的是什么?可以参考的参数在@CacheEvict“钥匙”的值,例如:

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

更多响应示例代码来响应低于大约具有使用用户对象的多个属性的多个高速缓存驱逐:

+0

“删除”返回的T类对象是这样的对象: class User用户ID userID; LoginName loginName; EmailAddress emailAddress; ... }。 我有多个缓存 - 一个是userID,另一个是loginName,另一个是emailAddress。当我通过userID删除一个条目时,我想要在其他两个缓存中删除条目......并且这些键都包含在返回值中,所以如果我可以这样做,它会很棒:@CacheEvict(key = “#returnValue.loginName”...),@CacheEvict(key =“#returnValue.emailAddress”...) –

+1

如果您传递User对象作为参数而不是UserID,那么您可以这样做然后引用SpEL中的不同用户属性。如果您需要执行多种不同配置的\ @ CacheEvict,则必须将其与\ @Caching注释组合使用: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference /html/cache.html#cache-annotations-caching – sdouglass

+0

根据您的评论,我编辑了一些更适合您情况的代码示例代码。传递整个用户的 – sdouglass

相关问题