I have a method (e.g. ValueResponse ApiClient#getValue()
) that calls an HTTP API which responds with something like this:
{
"value": "xyz",
"ttl": 3600
}
The ttl
in the JSON response (in seconds) is not constant and might change between invocations of the API.
I call this method from another method that I want to make @Cacheable
:
@Cacheable(...)
public ValueResponse someMethod() {
return apiClient.getValue();
}
I want ValueResponse
to be cached at most for the duration of its ttl
field (actually ttl - 60
seconds).
How can I do this with Spring's cache abstraction?
P.S: in case it matters, I am using EhCache for caching, but might switch to Redis or Memcached in the future.