Google Guava Cache returns records for SIZE if maxSize is not specified

I am using Google Guava CacheBuilder to create a Cache instance. My code looks something like this:

 return CacheBuilder.newBuilder() .initialCapacity(initialCapacity) .expireAfterAccess(expirationInterval, TimeUnit.SECONDS) .removalListener(LOGGING_AUTOEVICTION_ONLY_REMOVAL_LISTENER) .build(); 

My delete listener, as the name suggests:

 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 code above, I see the following log output:

19:08:03.287 DEBUG [MyCache] - Entry evicted from cache: MyKey[123] =MyRecord@43ee148b - Reason: SIZE

Why are the entries that go out of my cache when I did not use maximumSize() in the CacheBuilder call? I suspect this has something to do with weight. If so, what do I need to know about maxWeight() to not have cache entries evicted before expiration?

NOTE I know Cache Explained on Google Guava.

+4
source share
1 answer

Is it possible that your expirationInterval randomly zero? As the expireAfterAccess docs: "When duration is zero, this method is disabled to maximumSize(0) ."

+7
source

All Articles