I am using Spring Boot and for caching I am using Ehcache. It's working fine till now. But now I have to reload / refresh so how can I do this so that my application will not be having any downtime.
I have tried many ways in Spring Ehcache but it didn't work or Else have to write a scheduler and reload the data.
@Override
@Cacheable(value="partTypeCache", key="#partKey")
public List<PartType> loadPartType(String partKey) throws CustomException {
return productIdentityDao.loadPartType();
}
Apparently all the comments about your problem were right. You should use CacheEvict. I found solution here: https://www.baeldung.com/spring-boot-evict-cache and it looks like this:
All you have to do is create class called e.g. CacheService and create method that will evict all cached objects you have. Then you annotate that method @Scheduled and put your interval rate.
@Service
public class CacheService {
@Autowired
CacheManager cacheManager;
public void evictAllCaches() {
cacheManager.getCacheNames().stream()
.forEach(cacheName -> cacheManager.getCache(cacheName).clear());
}
@Scheduled(fixedRate = 6000)
public void evictAllcachesAtIntervals() {
evictAllCaches();
}
}
The option suggested by many to use @CacheEvict
is correct. Additionally, to ensure your cache (near) always has latest data loaded, even if data is updated in database out of bounds to your running app, you need to reload the whole dataset periodically with an interval matching your app's SLAs. In the solution suggested above, add logic to reload all data like below:
@Service
public class CacheService {
@Autowired
CacheManager cacheManager;
public void refreshAllCaches() {
cacheManager.getCacheNames().stream()
.forEach(cacheName -> cacheManager.getCache(cacheName).clear());
// reload whole dataset here, dummy example here:
dataRepository.findAll().forEach(a -> cacheManager.getCache("cache-name")).put(a.getKey(), a));
}
@Scheduled(fixedRate = 6000)
public void refreshAllcachesAtIntervals() {
refreshAllCaches();
}
}
Try something like this, as mentioned in comments also:
@Caching(evict={@CacheEvict(value="partTypeCache", key="#partKey")})
public boolean deletePartType(String partKey) {
//when this method is invoked the cache is evicted for the requested key
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With