How to Enable Cache to Set Changing Time to Lives #1834
-
Hi There I am playing around with Caffeine cache with some different use cases - and am not sure if this one in particular is achievable I have an api that allows users to publish messages (essentially key value data) and I am trying to also allow them to specify how often this data becomes invalid on a per message basis - essentially they will pass me a time to live I have seen that in Caffeine you can use the It seems as though when the cache is empty or the key does not currently exist then the Optional returned for Am i correct in thinking the above? Is it not possible to define an entirely empty cache with no default TTL, then for every put operation (whether the key is not present or whether it is an update) just provide the additional fields i need (the TTL) Hope that makes sense - may be a bit of a ramble! Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can use a default TTL that is essentially eternity, then use the Cache<Key, Graph> graphs = Caffeine.newBuilder()
.expireAfter(Expiry.creating((Key key, Graph graph) -> Duration.ofNanos(Long.MAX_VALUE)))
.build();
graphs.policy().expireVariably().orElseThrow()
.put(key, value, Duration.ofMinutes(30)); wdyt? |
Beta Was this translation helpful? Give feedback.
You can use a default TTL that is essentially eternity, then use the
VarExpiration
api to put the entry in with an explicit duration.wdyt?