PmanAce
PmanAce

Reputation: 4303

RedisCacheManager set expires certain time

I just started using RedisCacheManager, is there a way to set the cache to expire at a certain time, like midnight for example?

I can set the cache expiration for one day for example and everything works fine but I would like to make the cache expire at a certain time.

Thanks!

Upvotes: 1

Views: 959

Answers (2)

PmanAce
PmanAce

Reputation: 4303

I found a work-around using a scheduled task (spring), but an annotation would be best:

@Autowired
public ClearCacheTask(final @NotNull CacheManager cacheManager) {
  this.cacheManager = cacheManager;
}

@Scheduled(cron="0 0 0 * * ?")
@Async
public void resetCacheMidnight() {
  logger.info("Cron Task resetCacheMidnight(), clearing following caches...");

  cacheManager.getCacheNames().parallelStream().forEach(name ->  {
    cacheManager.getCache(name).clear();

    logger.info("...{} cache cleared", name);
  });      
}

Upvotes: 1

Christoph Strobl
Christoph Strobl

Reputation: 6726

At the time of writing there is no configuration option that would enable the desired behavior. I've opened DATAREDIS-772 to investigate on that issue. Please feel free to add thoughts there.

Upvotes: 2

Related Questions