I have a set of cached methods that look somewhat like this:
@Cacheable(value = "myCacheName", keyGenerator = "myKeyGenerator")
public Product getProduct(ProductRequest request) {
// ...
}
And I need to set different time to live (expiration interval) for objects returned by these methods.
Problem: According to the documentation, the offered way is using @RedisHash(timeToLive=…)
or @TimeToLive
annotations on the return type of the methods. However, I don't want to pollute my domain classes with caching related logic. In addition, some of my methods return strings or objects of classes which I can not modify. I would prefer to implement it in a more configurable way. There is also a configuration property called spring.cache.redis.time-to-live
, but it applies the same time-to-live in all places.
Question: Is there a way to specify time to live/expiration interval on the method level? Or generally, how to implement it in a more elegant way?