Redo the Spring cache?

Asked

Viewed 119 times

2

It is possible to schedule that the Spring cache is always redone at midnight?

I read the Springs Cache Docs and I found nothing about how to get him to be regenerated.

  • 1

    I think you are looking for this [https://spring.io/guides/gs/scheduling-tasks/]

  • Has the answer helped you to resolve the doubt? It is possible to accept it now?

  • Good afternoon, @Murilo, there have been other priorities over the past few weeks and the past. I’m coming back to this solution now and I’m already responding.

1 answer

3


Something that could be done is to use cache expiration (@CacheEvict) together with the scheduling (Scheduled), as below:

@Configuration
@EnableCaching
@EnableScheduling
public class CachingConfig {
    public static final String GAMES = "GAMES";
    @Bean
    public CacheManager cacheManager() {
        ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(GAMES);

        return cacheManager;
    }

    @CacheEvict(allEntries = true, value = {GAMES})
    @Scheduled(cron = "* * 0 * * ?")
    public void reportCacheEvict() {
        System.out.println("Flush Cache " + dateFormat.format(new Date()));
    }
}

Based in this answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.