Guava Cache Eviction Policy

I recently tried the guava cache and was surprised by the eviction policy. Although the cache is clearly indicated as lru in the documents, it is not a defect. For me, evictions look random, as my test shows. (the test is to add 100 ethnris, get 100 records, sweat 100 different records, check the eviction order). I would not want to detect some unexpected evictions at runtime. Could you provide some eviction policy prerequisites for a limited cache size. How to make the guava core evict like LHM?

+7
source share
1 answer

Guava keys are segmented in concurrencyLevel different hash tables to allow multiple simultaneous reads and writes. The default value of concurrencyLevel is 4. Basically, if your maximumSize set to 100 , then it only means that each of the four segments gets a maximumSize of 25. That's why the documentation for maximumSize

Please note that the cache may display a record before this limit is exceeded. As the cache size grows to its maximum, the cache crowds out entries that are less likely to be used again.

So, if by chance, there were 30 records that went into one particular segment, then 5 of these records would be evicted.

The only way to get global eviction with the least access for Cache is to completely disable concurrency by setting concurrencyLevel(1) . Even then, the documentation does not give any guarantees regarding the order of eviction of elements, and you should not depend on it.

+18
source

All Articles