@CacheEvict键不起作用

时间:2017-06-15 06:14:28

标签: spring caching spring-boot guava

当我将CacheEvict与所有条目一起使用时,它可以正常工作:

@CacheEvict(value = "users", allEntries = true)

但是当我想使用key删除给定的缓存条目时,它只是找不到密钥并抛出NullPointerException。我确定我传递了一个非空值作为参数。

@CacheEvict(value = "users", key = "#username")
void edit(String username, @Param(value = "user") User user);

java.lang.NullPointerException: null
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:782)
    at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:5080)
    at org.springframework.cache.guava.GuavaCache.evict(GuavaCache.java:139)
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doEvict(AbstractCacheInvoker.java:98)
    at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:480)
    at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:463)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:421)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)

...

2 个答案:

答案 0 :(得分:0)

按照 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html

根据条款: 可用缓存SpEL评估上下文

在这种情况下应该是: -

@CacheEvict(value = "users", key = "#user.username")
void edit(String username, @Param(value = "user") User user);

答案 1 :(得分:0)

使用p<#arg>表示法引用用户名。

例如 @CacheEvict(value = "users", key = "#p0")

应该做到这一点。

参考

https://docs.spring.io/spring/docs/4.3.15.RELEASE/spring-framework-reference/html/cache.html(表36.1。Cache SpEL可用的元数据)