caffeine: get API that does not update eviction heuristics
Is there a get API in caffeine that does not update the eviction heuristics used by Tiny-LFU? In my use case, my write operation has to read from the cache, but that would end up messing up the heuristics and not evicting the value even if I am only writing from it and not reading from it. Is there any way for the write thread to do a get() on a key, but not count that get as a cache hit?
Details: We are using Caffeine as a separate cache, away from the DB. However, it is somewhat atypical in that when a user reads a key, it typically requires loading N different entities from the DB. In cache, we store this aggregated wrapper containing these N entities, say the key we use for caching is UserId
, and the value stored in cache is a Map<EntityId, Entity>
.
For reads, app would talk to cache, and if not found, it would talk to the DB and put the value in cache. Writes only update a particular entity at a time, so a write is something like <UserId,EntityId,Entity>
. For this, we need to first check if the cache has any entries for UserId, and if there is an entry, do cache.get(UserId).put(entityId, entity)
. If the userId is not cached, then we don’t need to update cache. But this causes a problem because all writes are doing cache.get(userId)
, so caffeine will think that the key is getting accessed, but these are write accesses, and should not contribute to the entry staying in cache longer. We are only interested in keeping an entity if it is accessed at read time. Is there any way for us to let caffeine know the difference in access.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (12 by maintainers)
When the snapshot is built, please take a quick look to see if the new method works as desired in your application. This will be available on the Sonatype snapshot repo or by jitpack on master. If its a good match then I’ll cut a release.
Thanks, I have it all done with tests locally 😃