Caffeine: Is there any way to access the value without reseting its access time

Created on 28 Apr 2020  路  3Comments  路  Source: ben-manes/caffeine

Both following methods will reset the access time of the "key"
cache.getIfPresent("key"); cache.asMap().get("key");

We have set a duration of "expireAfterAccess", and on some special conditions, we want to get the cache, but do not reset its access time.

Most helpful comment

Released 2.8.3 which includes cache.policy().getIfPresentQuietly(key) which skips updating any cache metadata.

All 3 comments

I think I have got the answer: https://github.com/ben-manes/caffeine/issues/229

Ahh, I forgot about that one. Yes, the semantics becomes very confusing as to how quiet a method is.

For your case, the simplest workaround would probably require switching to expireAfter(Expiry). This would allow you to specify a custom strategy for determining the entry's remaining lifetime. A thread-local could capture that the duration shouldn't change and the prior value be returned. By setting the flag before the cache lookup, you could instruct your routine whether to reset the time or not.

Released 2.8.3 which includes cache.policy().getIfPresentQuietly(key) which skips updating any cache metadata.

Was this page helpful?
0 / 5 - 0 ratings