当从同一类的另一个方法调用缓存方法时,Spring缓存无法正常工作。
以下是一个示例,以清晰地解释我的问题。
缓存服务类:
class testServiceImpl{
@CachePut(key = "#result.id", condition = "#result != null")
public List<String> create(String input) {
......
}
@CacheEvict(key="#id")
public void deleteById(Long id) {
.....
}
public void multiDelete(String input) {
if(condition...){
deleteById(2); //Cache is not Evicted here i.e. the records are still present in getAll call but not in Database.
}else{
create(input); //New Data is persisted in DB but the same has not been updated in Cache.
}
@Transactional
@Cacheable
public Map<Long, String> getAll() {
...
}
我也尝试过以下解决方案,但都没有成功。
//Create a new object of the same class and use the same. In this case, the data is not persisted in DB i.e. it is not deleting the data from DB.
testServiceImpl testService;
...
public void multiDelete(String input) {
if(condition...){
testService.deleteById(2);
}else{
testService.create(input);
}
有人可以帮助我解决这个问题吗?