1

I'll try to keep my question short and simple.

Say, I have a Springboot application that allows creation of a certain entity with Time to live. I store the entity in a MongoDB. When a request comes to get the entity, the application fetches it from DB, caches it in Redis and sends the response. Next consecutive requests will be served from the cache

When the TTL (which is configurable and is determined at the time of creation of the entity) of the entity expires, it's going to be removed from the MongoDB.

I am having trouble figuring out how to sync that with the Redis cache. I was thinking that at the time of the first query when I am caching the response, I would cache it with the remaining TTL of the entity, so that I don't have to do anything extra to remove it from both Mongo and Redis. Both Redis and Mongo will take care of the removal on their own. Currently, I cache responses using the @Cacheable annotaion, but I couldn't find out any extra parameter in that annotation that will dynamically set a TTL.

Is there a way to achieve it keeping the @Cacheable annotation on my method?

Auro
  • 1,578
  • 3
  • 31
  • 62
  • How you are removing data from mongoDB when TTL occures? – Prabhat Feb 26 '20 at 16:48
  • @zombie by following https://stackoverflow.com/questions/43966601/spring-data-mongodb-how-to-assign-expiration-time-programmatically – Auro Feb 26 '20 at 16:52
  • Found something that might help you. https://stackoverflow.com/questions/9691316/how-to-listen-for-changes-to-a-mongodb-collection – Prabhat Feb 26 '20 at 17:26

0 Answers0