javaspringspring-cache

Schedule Spring cache eviction?


Is it possible to schedule spring cache eviction to everyday at midnight?

I've read Springs Cache Docs and found nothing about scheduled cache eviction.

I need to evict cache daily and recache it in case there were some changes outside my application.


Solution

  • Try to use @Scheduled Example:

    @Scheduled(fixedRate = ONE_DAY)
    @CacheEvict(value = { CACHE_NAME })
    public void clearCache() {  
        log.debug("Cache '{}' cleared.", CACHE);    
    }
    

    You can also use cron expression with @Scheduled.