jeudi 2 avril 2015

Ehcache Java Spring MVC and pagination @Cacheable

I use Ehcache 2.9.0 In my Spring Application and I would like to be able to cache a result based on the pagination.


First is how I use Ehcache right now:



@Cacheable(value ="EmployeesByCompanyId", key="#companyId")
public List<Employee> getEmployeesByCompanyId(String companyId) throws Exception {
return employeeRepository.getEmployeesByCompanyId(companyId);
}

@CacheEvict(value ="EmployeesByCompanyId", key="#company.id")
public Employee createNewEmployee(Employee employee, Company company)
{ ... }


Now is how I would like to use it for the same purpose but with a pagination. Example (not correct but it's the idea):



@Cacheable(value ="EmployeesByCompanyId", key="#companyId#page#maxResult"})
@Override
public List<Employee> getEmployeesByCompanyId(String companyId, int page, int maxResult) throws Exception {
return employeeRepository.getEmployeesByCompanyId(companyId, page, maxResult);
}


Then when I had a new employee to the list it should Evict the cache but I would like to evict the cache for every pages at once. If I write something like that would it remove the whole cache:



@CacheEvict(value ="EmployeesByCompanyId", key="#companyId#page#maxResult")


How should I do to make the caching work with the pagination and evict the whole cache related to every companyId and page when I add a new employee to this company?


Aucun commentaire:

Enregistrer un commentaire