163

I am trying out the @Cacheable annotation support for Spring 3.1 and wondering if there is any way to make the cached data clear out after a time by setting a TTL? Right now from what I can see I need to clear it out myself by using the @CacheEvict, and by using that together with @Scheduled I can make a TTL implementation myself but it seems a bit much for such a simple task?

12 Answers 12

82

Spring 3.1 and Guava 1.13.1:

@EnableCaching
@Configuration
public class CacheConfiguration implements CachingConfigurer {

    @Override
    public CacheManager cacheManager() {
        ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {

            @Override
            protected Cache createConcurrentMapCache(final String name) {
                return new ConcurrentMapCache(
                        name,
                        CacheBuilder.newBuilder()
                                .expireAfterWrite(30, TimeUnit.MINUTES)
                                .maximumSize(100)
                                .build()
                                .asMap(),
                        false
                );
            }
        };

        return cacheManager;
    }

    @Override
    public KeyGenerator keyGenerator() {
        return new DefaultKeyGenerator();
    }

}
6
  • 35
    For Spring 4.1 extend CachingConfigurerSupport and only overwrite cacheManager(). Commented Jun 30, 2016 at 16:12
  • 4
    @JohannesFlügel Why do not post any example code?
    – user16409822
    Commented Sep 14, 2021 at 5:23
  • @Henry It nearly looks the same like in the example above. Simply replace CacheConfiguration implements CachingConfigurer by CacheConfiguration extends CachingConfigurerSupport. The latter one already implements CachingConfigurer by default methods. So you don't need to implement them if you don't want to. Commented Sep 14, 2021 at 5:54
  • Thanks for reply. Sorry, but I am beginner and after trying many approach, I am afraid I could not find a proper solution. So, would you mind posting this as answer on my question --> stackoverflow.com/questions/69169170/…
    – user16409822
    Commented Sep 14, 2021 at 5:58
  • I used executor task with fixed delay to evict the cache. My requirement was dynamic setting of interval. Unless you use ehCache like framework, it's lot of work to achieve this. Commented Sep 21, 2023 at 15:34
56

I use life hacking like this

@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(fixedDelay = 10 * 60 * 1000 ,  initialDelay = 500)
public void reportCacheEvict() {
    System.out.println("Flush Cache " + dateFormat.format(new Date()));
}
6
  • Are you calling reportCacheEvict method from anywhere. How the cacheEvict happening??
    – Jaikrat
    Commented Dec 15, 2016 at 10:29
  • Get it. We are not calling this method from anywhere. Its called after fixedDelay time interval. Thanks for the hint.
    – Jaikrat
    Commented Dec 15, 2016 at 11:29
  • 8
    Clearing the entire cache on a schedule can be a handy hack to get things working, but this method can't be used to give items a TTL. Even the condition value can only declare whether to delete the entire cache. Underlying this is the fact that ConcurrentMapCache stores objects without any timestamp, so there's no way to evaluate a TTL as-is.
    – jmb
    Commented May 10, 2017 at 17:20
  • is code seat-of-the-pants (this method was scribbled down :) ).
    – Atum
    Commented May 15, 2017 at 5:34
  • Nice and clean approach
    – lauksas
    Commented Jul 3, 2019 at 20:09
50

See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#cache-specific-config:

How can I set the TTL/TTI/Eviction policy/XXX feature?

Directly through your cache provider. The cache abstraction is... well, an abstraction not a cache implementation

So, if you use EHCache, use EHCache's configuration to configure the TTL.

You could also use Guava's CacheBuilder to build a cache, and pass this cache's ConcurrentMap view to the setStore method of the ConcurrentMapCacheFactoryBean.

45

Here is a full example of setting up Guava Cache in Spring. I used Guava over Ehcache because it's a bit lighter weight and the config seemed more straight forward to me.

Import Maven Dependencies

Add these dependencies to your maven pom file and run clean and packages. These files are the Guava dep and Spring helper methods for use in the CacheBuilder.

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>

Configure the Cache

You need to create a CacheConfig file to configure the cache using Java config.

@Configuration
@EnableCaching
public class CacheConfig {

   public final static String CACHE_ONE = "cacheOne";
   public final static String CACHE_TWO = "cacheTwo";

   @Bean
   public Cache cacheOne() {
      return new GuavaCache(CACHE_ONE, CacheBuilder.newBuilder()
            .expireAfterWrite(60, TimeUnit.MINUTES)
            .build());
   }

   @Bean
   public Cache cacheTwo() {
      return new GuavaCache(CACHE_TWO, CacheBuilder.newBuilder()
            .expireAfterWrite(60, TimeUnit.SECONDS)
            .build());
   }
}

Annotate the method to be cached

Add the @Cacheable annotation and pass in the cache name.

@Service
public class CachedService extends WebServiceGatewaySupport implements CachedService {

    @Inject
    private RestTemplate restTemplate;


    @Cacheable(CacheConfig.CACHE_ONE)
    public String getCached() {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> reqEntity = new HttpEntity<>("url", headers);

        ResponseEntity<String> response;

        String url = "url";
        response = restTemplate.exchange(
                url,
                HttpMethod.GET, reqEntity, String.class);

        return response.getBody();
    }
}

You can see a more complete example here with annotated screenshots: Guava Cache in Spring

1
41

Springboot 1.3.8

import java.util.concurrent.TimeUnit;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.cache.CacheBuilder;

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

@Override
@Bean
public CacheManager cacheManager() {
    GuavaCacheManager cacheManager = new GuavaCacheManager();
    return cacheManager;
}

@Bean
public CacheManager timeoutCacheManager() {
    GuavaCacheManager cacheManager = new GuavaCacheManager();
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
            .maximumSize(100)
            .expireAfterWrite(5, TimeUnit.SECONDS);
    cacheManager.setCacheBuilder(cacheBuilder);
    return cacheManager;
}

}

and

@Cacheable(value="A", cacheManager="timeoutCacheManager")
public Object getA(){
...
}
1
  • 1
    Amazing! This was exactly what i was looking for
    – MerLito
    Commented May 24, 2017 at 18:05
11

This can be done by extending org.springframework.cache.interceptor.CacheInterceptor, and override doPut method - org.springframework.cache.interceptor.AbstractCacheInvoker your override logic should use the cache provider put method that knows to set TTL for cache entry (in my case I use HazelcastCacheManager)

@Autowired
@Qualifier(value = "cacheManager")
private CacheManager hazelcastCacheManager;

@Override
protected void doPut(Cache cache, Object key, Object result) {
        //super.doPut(cache, key, result); 
        HazelcastCacheManager hazelcastCacheManager = (HazelcastCacheManager) this.hazelcastCacheManager;
        HazelcastInstance hazelcastInstance = hazelcastCacheManager.getHazelcastInstance();
        IMap<Object, Object> map = hazelcastInstance.getMap("CacheName");
        //set time to leave 18000 secondes
        map.put(key, result, 18000, TimeUnit.SECONDS);

   
   
}

on your cache configuration you need to add those 2 bean methods , creating your custom interceptor instance .

@Bean
public CacheOperationSource cacheOperationSource() {
    return new AnnotationCacheOperationSource();
}


@Primary
@Bean
public CacheInterceptor cacheInterceptor() {
    CacheInterceptor interceptor = new MyCustomCacheInterceptor();
    interceptor.setCacheOperationSources(cacheOperationSource());    
    return interceptor;
}

This solution is good when you want to set the TTL on the entry level, and not globally on cache level

2
  • I am trying to follow your approach and created my own cache interceptor by extending inbuilt one. But when actually "Cacheable" annotation gets executed, it keeps calling in-built interceptor and never calls my own interceptor. Do I need to change any setting anywhere apart from putting @Primary tag on my method? Commented Aug 20, 2021 at 15:08
  • try to add this to your application.properties - so at run time it will use your config, spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration ,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration - it means that your config is responsible to create also the CacheManager @Bean
    – lukass77
    Commented Aug 24, 2021 at 18:47
8

When using Redis, TTL can be set in properties file like this:

spring.cache.redis.time-to-live=1d # 1 day

spring.cache.redis.time-to-live=5m # 5 minutes

spring.cache.redis.time-to-live=10s # 10 seconds

2
  • Much simplier with redis
    – cpxondo
    Commented Dec 27, 2021 at 15:00
  • 1
    This is a bit limiting since it will set only one TTL. In real world app you will want to have different TTL for different cache entries.
    – Alexis
    Commented Jul 12, 2022 at 11:55
6

The easiest way for me was using the Caffeine cache which is configurable directly in your application.yml file.

You can setup the TTL by the expireAfterWrite parameter. Example configuration would look as follows:

spring:
  cache:
    caffeine:
      spec: expireAfterWrite=15m
    cache-names: mycache

This will evict the elements after 15 minutes.

2
  • 3
    Is this evict all cached data in mychace or just items that wrote 15m ago?
    – sansari
    Commented Jul 20, 2022 at 10:33
  • @sansari every entry has its own ttl. so this will evict an entry after it was written after 15m. other items would not be affected until their ttl is expired as well. does that help? Commented Nov 9, 2023 at 13:32
2

Since Spring-boot 1.3.3, you may set expire time in CacheManager by using RedisCacheManager.setExpires or RedisCacheManager.setDefaultExpiration in CacheManagerCustomizer call-back bean.

2

Redis based solution

  • Spring boot: 2.2.6
  • Cache store: Redis (spring-boot-starter-cache along with spring-boot-starter-data-redis)
  • Java version: 8

Implementation:

import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;

import java.time.Duration;

@Configuration
public class CacheConfig {

    @Bean
    public RedisCacheManagerBuilderCustomizer customizer() {
        return builder -> builder
                .withCacheConfiguration("cacheKey1",
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(14)))
                .withCacheConfiguration("cacheKey2",
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)))
                .withCacheConfiguration("cacheKey3",
                        RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1)));
    }
}
2
  • It is also compatible with properties from application.yml, so you can combine them Commented May 12, 2022 at 13:18
  • 1
    This forces us to use a cacheName that we don't really want. I prefer to have different cacheManager defined so that I have control on the cache name.
    – Alexis
    Commented Jul 12, 2022 at 11:56
1
@CacheEvict(value = "hotels", allEntries = true)
@Scheduled(fixedRateString = "${caching.spring.hotelListTTL}")
public void emptyHotelsCache() {
    logger.info("emptying Hotels cache");
}

More here: https://www.baeldung.com/spring-setting-ttl-value-cache

-2

If you are working with redis and Java 8, you can take a look at JetCache:

@Cached(expire = 10, timeUnit = TimeUnit.MINUTES) User getUserById(long userId);

2
  • 1
    question is for spring @Cacheable annotation
    – satyesht
    Commented Jun 24, 2019 at 10:35
  • 1
    @satyesht Not sure why need to put -5 vote though.
    – Arefe
    Commented Apr 19, 2021 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.