Spring cache is not working when calling cached method from another method of the same class.
Here is an example to explain my problem in clear way.
Cached service class:
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() {
...
}
I have tried using the below solutions as well but could not succeed.
//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);
}
Can someone please help me to solve this issue ?