2014-08-29 99 views
1

我使用Spring 3.2缓存抽象使用ehcache作为实现。Ehcache:从缓存中删除条目存储为列表

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

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(value = "empCache", key="#empId") 
public void deleteEmployee(int empId) { 

//deletes employee object  
} 

回答

0

看着春天的Cache abstraction文档中给出的代码通过@CacheEvict删除,您使用@Cacheable定义的内容是缓存中的一个条目,映射到密钥SimpleKey.EMPTY。情况就是这样,因为你的方法没有参数。 但那么您的@CacheEvict被定义为在密钥empId上工作。所以你在两个操作之间不匹配。但是,除了这种不匹配之外,我不相信你想要做的是Spring的Cache抽象支持。 当你考虑它时,你需要告诉Spring如何根据其empId识别列表中的员工。