Skip to content
Ben Manes edited this page Apr 8, 2015 · 8 revisions

The eviction policies that the cache supports are fixed at construction. At runtime this configuration can be inspected and adjusted. The policies are obtained through an Optional to indicate whether the feature is supported by the cache.

Size-based

cache.policy().eviction().map(eviction -> {
  eviction.setMaximum(2 * eviction.getMaximum());
});

If the cache is bounded by a maximum weight, the current weight can be obtained using weightedSize(). This differs from Cache.estimatedSize() which reports the number of entries present.

The maximum size or weight can be read from getMaximum() and adjusted using setMaximum(long)`. The cache will evict until it is within the new threshold.

If the subset of entries most likely to be kept or evicted is needed, the coldest(int) and hottest(int) methods provide an ordered snapshot of the entries.

Time-based

cache.policy().expireAfterAccess().map(expiration -> ...);
cache.policy().expireAfterWrite().map(expiration -> ...);
cache.policy().refreshAfterWrite().map(expiration -> ...);

The ageOf(key, TimeUnit) provides how long an entry has been idle from the perspective of the expireAfterAccess, expireAfterWrite, or refreshAfterWrite policy. The maximum duration can be read from getExpiresAfter(TimeUnit) and adjusted using setExpiresAfter(long, TimeUnit).

If the subset of entries most likely to be retained or expired is needed, the youngest(int) and oldest(int) methods provide an ordered snapshot of the entries.

Clone this wiki locally