TL;DR
Eviction?
Eviction means "expulsion" in Japanese.
In the cache context, when you try to store new data beyond what the cache can hold, the strategy is "Which data should I erase to get more space to store in the new cache?" ..
The upper limit indicated by "exceeding the capacity that can be held in the cache" is the number of entries to be held in the cache, the size of the object, and the setting of the upper limit varies depending on the product that handles the cache.
Please note that it is different from Expire.
If you look at Wikipedia, you will find an example of Eviction Policy.
[Cache Algorithm](https://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5%E3% 82% A2% E3% 83% AB% E3% 82% B4% E3% 83% AA% E3% 82% BA% E3% 83% A0)
For example, LRU, LFU, ARC, etc.
LRU(Least Recently Used)
LRU is an algorithm that targets the least used data for deletion.
It is a very famous and easy-to-understand algorithm, but for example, in the case of accessing most of the data stored in the cache because it is batch processing, it may not be a very efficient use of the cache.
LFU(Least Frequently Used)
LFU is an algorithm that keeps data access frequency and targets infrequently used data for deletion.
Caffeine
From Caffeine, a recent famous place.
Caffeine is a cache library inspired by Google Guava Cache.
The Eviction Policy implemented in Caffeine is TinyLFU.
Caffeine uses the Window TinyLfu policy due to its high hit rate and low memory footprint.
Ehcache 3
At the time of Ehcache 2, the cache library in Java was Ehcache, but now it seems that I haven't heard much about it.
In Ehcache 3, the Eviction Policy is gone. It seems to conclude that it is difficult to implement Eviction efficiently for situations where there are storage destinations such as heap and off-heap.
How to specify ehcache3 eviction strategy when cache is full ?
By the way, around the time of Ehcache 2.x, there were LRU, LFU, and FIFO.
Built-in Memory Store Eviction Algorithms
Even with Ehcache 3, it seems that you can control it at the Persistence layer by creating an Eviction Advisor. This leads to the story about the save destination.
In other words, let's make it ourselves according to the destination where the cache entry is saved.
Google Guava Cache
Finally, Google Guava Cache. Now that Caffeine has arrived, you may not use it.
In the case of Guava's Cache, you can set the Eviction by size, time, and reference (although there are some that say Eviction but Expire?).
Of these, using SoftReference as a reference seems to be LRU-like.
I think it's a good idea to keep this information as a starting point.
Recommended Posts