【问题标题】:@CacheEvict key not working@CacheEvict 键不起作用
【发布时间】:2017-06-15 06:14:27
【问题描述】:

当我对所有条目使用 CacheEvict 时,一切正常:

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

但是当我想使用 key 来删除给定的缓存条目时,它只是找不到 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)

...

【问题讨论】:

    标签: spring caching spring-boot guava


    【解决方案1】:

    使用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. 缓存 SpEL 可用元数据)

    【讨论】:

      【解决方案2】:

      根据 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);
      

      【讨论】:

      • 我想使用第一个参数作为键,所以键名应该是“#username”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 2021-01-28
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多