Reputation: 10311
I am using the Google Guava CacheBuilder
to create a Cache
instance. My code looks something like the following:
return CacheBuilder.newBuilder()
.initialCapacity(initialCapacity)
.expireAfterAccess(expirationInterval, TimeUnit.SECONDS)
.removalListener(LOGGING_AUTOEVICTION_ONLY_REMOVAL_LISTENER)
.build();
My removal listener is, as the name implies:
private static final RemovalListener<MyKey, MyRecord> LOGGING_AUTOEVICTION_ONLY_REMOVAL_LISTENER
= new RemovalListener<MyKey, MyRecord>() {
@Override
public void onRemoval(RemovalNotification<MyKey, MyRecord objectObjectRemovalNotification) {
if (objectObjectRemovalNotification.wasEvicted()) {
log.debug("Entry evicted from cache: {} - Reason: {}", objectObjectRemovalNotification, objectObjectRemovalNotification.getCause());
}
}
};
Finally, I only ever get
entries to my cache, using get(K key, Callable<? extends V> valueLoader)
Here's the problem: with the above code, I am seeing the following log output:
19:08:03.287 DEBUG [MyCache] - Entry evicted from cache: MyKey[123]=MyRecord@43ee148b - Reason: SIZE
Why are records being evicted from my cache when I didn't use maximumSize()
in the CacheBuilder invocation? I suspect it has something to do with "weight." If so, what do I need to know about maxWeight()
to not have cache entries evicted before the expiration time?
NOTE I'm aware of Caches Explained on the Google Guava site.
Upvotes: 4
Views: 2298
Reputation: 40851
Is it possible that your expirationInterval
is accidentally equal to zero? As expireAfterAccess
documents, "When duration
is zero, this method hands off to maximumSize(0)
."
Upvotes: 7