gosachin1
gosachin1

Reputation: 71

com.github.benmanes.caffeine.cache.maximumsize(long maximumSize) allows max records up to record-count of maximumSize?

https://www.javadoc.io/doc/com.github.ben-manes.caffeine/caffeine/1.0.0/com/github/benmanes/caffeine/cache/Caffeine.html#maximumSize-long-

says that maximumSize() specifies the maximum number of entries the cache may contain. Note that the cache may evict an entry before this limit is exceeded..

Will it exhibit same behavior that we saw in GuavaCache where max count was much lesser than expected due to default ConcurrencyLevel being as 4 ?

Upvotes: 1

Views: 1975

Answers (1)

Ben Manes
Ben Manes

Reputation: 9621

This wording is to provide implementation flexibility. In the latest version it reads,

Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting.

Guava uses a segmented hash table, where each segment evicts independently. This results in evicting from one segment even if space is available in another, since they are independent. Due to being coupled into the hash table, it also ensures that the segment cannot exceed its limit even temporarily.

Caffeine does not segment the eviction policy and does not use a custom hash table. This allows it to reach the full capacity before an eviction is triggered, but does mean that it will exceed the maximum temporarily. The hash table and eviction policy are decoupled, where a write occurs on the hash table and is then communicated to the policy via a writeBuffer. This is a bounded buffer that is drained immediately, but if it is somehow filled then it incurs back pressure on writers to allow the evictions to keep up. Thus the cache may exceed the maximum by a small margin under load, but will never have runaway growth.

The migration guide summarizes the important changes to be aware of.

Maximum size (or weighted size)

Guava will evict prior to reaching the maximum size using the LRU algorithm. Caffeine will evict once the threshold has been crossed using the Window TinyLFU algorithm.

Upvotes: 2

Related Questions