Spring 缓存逐出
Spring caching eviction
我无法理解 spring @cacheEvict 注释。它是触发缓存逐出的方法吗?请看下面。
@CacheEvict
public void clearEmployeeById(int id) {
//Do we have to add a method as trigger here in order to trigger the cache eviction? or can we leave this method without implementation
}
``
您需要在@CacheEvict
中指定缓存名称
@CacheEvict(value = {"employee"}, allEntries = true)
public void clearEmployeeById(int id) {
//Logic
}
缓存 employee
将获得 evicted
,只要 clearEmplyoeeByID()
方法被调用。
将缓存添加到 employee
@Cacheable(value = {"employee"})
public Employee addEmployeeById(int id) {
//Logic
return employee;
}
要清除所有缓存,您需要使用CacheManager
@Autowired
private CacheManager cacheManager;
@Scheduled(fixedDelay = 86400000)
public boolean evictAllCaches() {
cacheManager.getCacheNames().stream().forEach(c -> cacheManager.getCache(c).clear());
return true;
}
我无法理解 spring @cacheEvict 注释。它是触发缓存逐出的方法吗?请看下面。
@CacheEvict
public void clearEmployeeById(int id) {
//Do we have to add a method as trigger here in order to trigger the cache eviction? or can we leave this method without implementation
}
``
您需要在@CacheEvict
@CacheEvict(value = {"employee"}, allEntries = true)
public void clearEmployeeById(int id) {
//Logic
}
缓存 employee
将获得 evicted
,只要 clearEmplyoeeByID()
方法被调用。
将缓存添加到 employee
@Cacheable(value = {"employee"})
public Employee addEmployeeById(int id) {
//Logic
return employee;
}
要清除所有缓存,您需要使用CacheManager
@Autowired
private CacheManager cacheManager;
@Scheduled(fixedDelay = 86400000)
public boolean evictAllCaches() {
cacheManager.getCacheNames().stream().forEach(c -> cacheManager.getCache(c).clear());
return true;
}