1

我正在使用使用 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

}

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

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

//deletes employee object       
}   
4

1 回答 1

0

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

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

于 2014-09-01T09:50:49.377 回答