Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava cache eviction policy

Tags:

guava

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?

like image 625
Alexander Ashitkin Avatar asked Apr 19 '12 20:04

Alexander Ashitkin


1 Answers

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.

like image 194
Louis Wasserman Avatar answered Nov 14 '22 19:11

Louis Wasserman