chaitanya
chaitanya

Reputation: 421

Spring Boot Custom Cache TTL

I am new to cache technology in spring boot where I am using @cache in my project. Now I have a new requirement where the cache TTL need to be set and that TTL has to extend on key basis.

E.g. if I set a cache expiry time as 5 minutes, before the cache is getting expired it has to check whether the incoming key is present in the cache or not if it is present in the cache the TTL has to extends for next 5 minutes else the cache should be cleared or evicted and fresh cache should be created.

I hope I am clear with my requirement, please provide me solution with perfect example which suits my requirement. I have referred some links like Ecache custom expiration but I am confused on how to pass the custom expiry on key basis.

http://www.ehcache.org/documentation/3.1/expiry.html

Thanks in advance for guiding me,
Chaitanya

Upvotes: 0

Views: 1477

Answers (1)

anand1st
anand1st

Reputation: 1176

With spring-boot, you'll need to use @Cacheable on your method calls e.g.

@Cacheable(names = "eventCache", key = "#eventId")
Event getByEventId(String eventId);

Prior to this, you'll need to create a Spring CacheManager bean with your caches initialized. Please refer to this StackOverflow link on how to do this with EhCache and creating your cache instances.

Upvotes: 0

Related Questions