I tried guava cache recently and was surprised by eviction policy. Although the cache is clearly stated as an lru in docs, but it isn't defacto. For me evictions looks random as my test shows. (the test is to add 100 etnries, get 100 entries, pot different 100 entries, check eviction order) I'd not like to detect some unexpected evictions in runtime. Could you please provide some background behind eviction policy for a size limited Cache. How can i force guava cache to evict like LHM does?
Guava caches are segmented into concurrencyLevel
different hash tables to allow multiple concurrent reads and writes. The default concurrencyLevel
is 4. Basically, if your maximumSize
is set to 100
, then that actually only results in each of the four segments getting a maximumSize
of 25. This is why the maximumSize
documentation states:
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.
So if by chance, there were 30 entries that went into one particular segment, then 5 of those entries will get evicted.
The only way to get global least-recently-accessed eviction for a Cache
is to turn off concurrency entirely by setting concurrencyLevel(1)
. Even then, the documentation does not make any guarantees on the eviction order of elements, and you should not depend on it.
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