@Cacheable(cacheNames = "BooksCache", key = "#id")
public Book findById(long id) {
LOGGER.info("Fetching Book From DB For BookId: {}", id);
return bookRepository.findById(id).orElse(null);
}
Cacheable is working fine but when we add a new book or update the existing book cache is not updating
Here is the code for saveOrUpdate() and I have used @CachePut to update the cache but that was not working, Database is getting updated but cache is not updating
@Transactional
@CachePut(cacheNames = "BooksCache", key = "#book.id")
public Book saveOrUpdateBook(Book book) {
return bookRepository.save(book);
}
I have tried @EnableTransactionManagement along with @Transactional Annotation. I have also tried spring boot starter cache instead of redis cache, But that was not working
CodePudding user response:
You must call @Cachable- methods from another class. Otherwise the cache proxy will not work and the cache doesn't change/trigger.