Spring @Cacheable default TTL

Anand Sunderraman picture Anand Sunderraman · May 26, 2017 · Viewed 33.6k times · Source

I generally use the @Cacheable with a cache config in my spring-boot app and set specific TTL (time to live) for each cache.

I recently inherited a spring boot app that uses @Cacheable without explicitly stating a cache manager and ttl. I will be changing it to be explicit.

But I am not able to find out what are the defaults when there is nothing explicit.

I did look at the docs but found nothing there

Answer

Arpit Aggarwal picture Arpit Aggarwal · May 26, 2017

Spring @Cacheable does not have any configurable option to set TTL for the cache though you can build it using @CacheEvict and @Scheduled, as follows:

@CacheEvict(allEntries = true, cacheNames = { "cache_1", "cache_2" })
@Scheduled(fixedDelay = 30000)
public void cacheEvict() {
}

You can find detailed solution/explanation here - Setting TTL for @Cacheable – Spring.