0

I have the following cache implementation in a Spring Boot app and it is working without any problem. However, I want to define expiration for this approach. Is it possible to set expiration for @Cacheable?

I look at Expiry time @Cacheable spring boot and there is not seem to be a direct way for @Cacheable. Is it possible via a smart approach?

@Configuration
@EnableCaching
public class CachingConfig {

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }
}
@Component
public class SimpleCacheCustomizer 
  implements CacheManagerCustomizer<ConcurrentMapCacheManager> {

    @Override
    public void customize(ConcurrentMapCacheManager cacheManager) {
        cacheManager.setCacheNames(asList("users"));
    }
}
@Cacheable("users")
public List<User> getUsers(UUID id) {...}

1 Answers1

1

As said in the Spring documentation, there is no TTL for the default cache system of Spring.

8.7. How can I Set the TTL/TTI/Eviction policy/XXX feature?

Directly through your cache provider. The cache abstraction is an abstraction, not a cache implementation. The solution you use might support various data policies and different topologies that other solutions do not support (for example, the JDK ConcurrentHashMap — exposing that in the cache abstraction would be useless because there would no backing support). Such functionality should be controlled directly through the backing cache (when configuring it) or through its native API

You'll have to use an other cache provider like Redis or Gemfire if you want a TTL configuration.

An example of how to use TTL with Redis is available here.

Pilpo
  • 1,236
  • 1
  • 7
  • 18
  • Thanks a lot,I also realized it. But how can I use Redis or Gemfire in my implementation above? I tried to use another one as mentioned on https://stackoverflow.com/questions/44202700/spring-cacheable-default-ttl, but now worked –  Sep 14 '21 at 07:20
  • Could you post as an example code by integrating it to the approach above? –  Sep 14 '21 at 07:20
  • 1
    I've answered your question. If you have a problem with a `Redis` configuration, you should create an other post and show us what you did and what did not work. In the meantime, I can give this URL https://www.baeldung.com/spring-boot-redis-cache which show you how to use it. – Pilpo Sep 14 '21 at 07:23
  • Thanks a lot. In this scene I need to use other cache providers instead of `@Cacheable` I think. So, which one should I prefer for my Spring Boot? Redis, EhCache, JCache, etc. ? –  Sep 14 '21 at 07:26
  • 1
    For large scale distributed app, you should prefer `Redis`, otherwise, you can pick `EhCache` for a simple quick in-memory cache. Both are good. – Pilpo Sep 14 '21 at 07:30
  • You're welcome, don't forget to accept the answer if it solve your first issue. – Pilpo Sep 14 '21 at 07:31
  • I tried that approach on Baeldung, but it gives the following error: "org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: –  Sep 14 '21 at 08:02
  • 1
    I can't help you much without the code, but it seems you need to make an object serializable. – Pilpo Sep 14 '21 at 08:13
  • I use another example on https://www.baeldung.com/spring-boot-redis-cache. I hope it is also a good example for Redis. Any idea? –  Sep 14 '21 at 12:22