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.
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.
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.
Hazelcast is for distributed caching, meaning many services share the same cache, whereas Guava/Caffeine is a local cache per each service (not shared).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With