Do Guava Causes Consider Weight in Choosing Eviction?

The latest version of the excellent Guava library has updated apis caching. CacheBuilder now has a maxmimumWeight () method to provide maximum weight over the cache. Javadoc state:

Defines the maximum weight of entries that the cache may contain. the weight is determined using the Weiger indicated by the weight dispenser, and using this method requires an appropriate call before weighing before calling build (com.google.common.cache.CacheLoader).

Please note that the cache may display a record before this limit is exceeded. As the cache size increases to the maximum, the cache crowds out entries that are less likely to be used again. For example, a cache may evict a record because it has not been used recently or very often.

When a cache needs to be evicted, will it consider the weight? For example, it might be better to keep several records with a small weight than one record of a large weight, if a record of large weight is used more often than any small object, but less often than all small objects together.

+7
source share
1 answer

Guava team member here.

As the cache size grows to its maximum, the cache crowds out entries that are less likely to be used again. For example, a cache may display a record because it has not been used recently or very often.

If the cache has any other behavior, it is not documented (and should not be relied on). However, the current implementation only cares that access has recently been gained if you look at the source :

while (totalWeight > maxSegmentWeight) { ReferenceEntry<K, V> e = getNextEvictable(); if (!removeEntry(e, e.getHash(), RemovalCause.SIZE)) { throw new AssertionError(); } } 

and getNextEvictable iterates in the order of least available access.

+12
source

All Articles