Heyho,
currently using guava to cache users. So my question is whether it is possible with guava to rescrict the eviction of an entry if the cached object has a specific attribute value. For example if user.isOnline() returns true the user is not evicted even if he hasn't been accessed for a specific amount of time (.expireAfterAccess(KEEP_LOADED, TimeUnit.SECONDS)).
EDIT:
I found this in the javadoc of CacheBuilder.weighter(...):
When the weight of an entry is zero it will not be considered for size-based eviction (though it still may be evicted by other means).
But I don't want to evict my user in any way if he is online.
Basically I cache my users this way:
this.cache = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHED_USERS)
.expireAfterAccess(KEEP_LOADED, TimeUnit.SECONDS)
.build(new UserLoader(core));
EDIT:
ok, I could use the weighter to limit the size of my cache. For every user who is online I could return 0 and for every else 1. This would cause the cache to keep online users. But if would be nice to use maximumSize and expireAfterAccess. But CacheBuilder.weighter(...) does not block expireAfterAccess(...) setting to evict old users.
EDIT:
Maybe it's possible to cancel the eviction in some way, but I'm not sure how :/
Related to: Is it safe to reinsert the entry from Guava RemovalListener?
Max