【问题标题】:Ehcache: Deleting entries from cache stored as listEhcache:从存储为列表的缓存中删除条目
【发布时间】:2014-10-23 06:22:12
【问题描述】:

我正在使用使用 ehcache 作为实现的 Spring 3.2 缓存抽象。

我能够缓存返回对象列表的方法的输出,如下所示。

public Class Employee
  {
       private int empId;
       private String name;

      //getters and setters
 }

@Cacheable(value = "empCache")
public List<Employee> getAllEmployess() {

        //method queries the db and returns a list of all employees

}

但我无法使用下面给出的代码通过@CacheEvict 在更新或删除时从缓存中存储的List&lt;Employee&gt; 对象中删除特定条目

@CacheEvict(value = "empCache", key="#empId")
public void deleteEmployee(int empId) {

//deletes employee object       
}   

【问题讨论】:

    标签: caching ehcache spring-cache


    【解决方案1】:

    查看 Spring 的 Cache abstraction 文档,您使用 @Cacheable 定义的是缓存中的一个条目,映射到键 SimpleKey.EMPTY。之所以如此,是因为您的方法没有参数。 但是你的@CacheEvict 被定义为在密钥empId 上工作。所以你有两个操作之间的不匹配。

    但是,除了这种不匹配之外,我不相信 Spring 的 Cache 抽象支持您尝试做的事情。 当您考虑它时,您需要告诉 Spring 如何根据其empId 识别列表中的员工。

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 2013-09-16
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      相关资源
      最近更新 更多