1
votes

I am using Spring 3.2 Cache abstraction using ehcache as the implementation.

I am able to cache the output of a method which returns a List of objects as illustrated below.

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

}

But i am unable to remove a particular entry from the List<Employee> object stored in the cache at times of update or delete through @CacheEvict using the code given below

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

//deletes employee object       
}   
1

1 Answers

0
votes

Looking at Spring's Cache abstraction documentation, what you are defining with @Cacheable is one entry in the cache, mapped to the key SimpleKey.EMPTY. That is the case because your method has no parameters. But then your @CacheEvict is defined to work on key empId. So you have a mismatch between the two operations.

However in addition to this mismatch, I do not believe what you are trying to do is supported by Spring's Cache abstraction. When you think about it, you would need to tell Spring how to identify the employee in the list based on its empId.