Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Guava caches consider weight in eviction choices?

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

Specifies the maximum weight of entries the cache may contain. Weight is determined using the Weigher specified with weigher, and use of this method requires a corresponding call to weigher prior to calling build(com.google.common.cache.CacheLoader).

Note that the cache may evict an entry before this limit is exceeded. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.

When the Cache needs to evict an entry, will it consider the weights? For example, it may be better to keep several entries of small weight than one entry of large weight, if the large weight entry is used more often than any small item, but less often than all of the small items together.

like image 438
Dave L. Avatar asked Mar 29 '12 18:03

Dave L.


People also ask

How does Guava cache work?

Guava provides a very powerful memory based caching mechanism by an interface LoadingCache<K,V>. Values are automatically loaded in the cache and it provides many utility methods useful for caching needs.

What are cache eviction techniques?

Cache eviction is a feature where file data blocks in the cache are released when fileset usage exceeds the fileset soft quota, and space is created for new files. The process of releasing blocks is called eviction. However, file data is not evicted if the file data is dirty.

Is Google Guava cache distributed?

Hazelcast is for distributed caching, meaning many services share the same cache, whereas Guava/Caffeine is a local cache per each service (not shared).

What is Guava loading cache?

A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated. Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.


1 Answers

Guava team member here.

As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.

If the cache has any other behavior, it's not documented (and shouldn't be relied on). That said, the current implementation only cares about most-recently accessed, 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 order of least-recently-accessed.

like image 97
Louis Wasserman Avatar answered Sep 23 '22 16:09

Louis Wasserman