I am trying to implement Spring boot cacheable. I want to cache the method response which as ws call.
1) I am able to achieve caching upon request.
@Cacheable(cacheNames = "mycache", key = "#root.target.cacheKey")
public String myMethod() {
}
2) I am scheduling cache evict every day at after 1 AM.
@Scheduled(cron = "${1 AM}")
@CacheEvict(cacheNames = "mycache", key = "#root.target.CACHE_KEY")
public void clearCache() {
LOGGER("Cache eviction:: ");
}
This is also working fine. My question is after evicting without any requests from the browser to @Cacheable annotated method can I call @Cacheable annotated method post evict like to ensure for application safety?
@Scheduled(cron = "${1 AM}")
@CacheEvict(cacheNames = "mycache", key = "#root.target.CACHE_KEY")
public void clearCache() {
LOGGER("Cache eviction:: ");
myMethod();
}
This is for application safety .Incase @Cacehable fails to cache response it won't impact the application. I do agree that first request after evict will definitely get inside @Cacheable annotated method and add to cache .But need to make sure that I am following the right approach
Can some one shed some light so that will be useful for me to correct