1

I have followed some approaches on Can I set a TTL for @Cacheable and similar pages on the web, but most of them are not working or outdated.

I need to set expiration time for my @Cacheable implementation, but unfıortunately there is not a proper solution for that. So, could you please clarify me how can I set TTL for @Cacheable?

My implementation is the same as mentioned for Spring Boot on Baeldung. Any help would be appreciated.

Jack
  • 1
  • 21
  • 118
  • 236
  • You cannot, nor should, set the TTL through `@Cacheable`. How to configure the TTL depends on your cache implementation and you should refer to that documentation. The "cache" used in that tutorial isn't really a cache that should be used in production, it is basically a `Map`, if you want a real cache use something like EhCache, JCache etc. which all provide configuration options for that. – M. Deinum Sep 14 '21 at 07:19
  • Thanks for reply. Which one (EhCache, JCache, etc.) would you suggest for a Spring Boot app? Which one is better? On the other hand, is there a simple implementation for that? – Jack Sep 14 '21 at 07:25
  • I'm not going to recommend a caching implementation as that depends on your needs. Spring provides abstractions for several implementation and Spring Boot will auto configure those. – M. Deinum Sep 14 '21 at 07:29
  • What about **Redis**? Would you suggest the approach on https://www.baeldung.com/spring-boot-redis-cache ? – Jack Sep 14 '21 at 07:29
  • No sorry, as which cache to use depends on so much variables and it is an opinionated topic. – M. Deinum Sep 14 '21 at 07:32
  • At least you can say that if using Redis for a mid level app may be good idea or not. – Jack Sep 14 '21 at 07:45

0 Answers0