Java cache library and Eviction Policy

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.

Typical Eviction Policy

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.

Famous place

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.

Eviction Policy implemented in Java cache library

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.

Efficiency

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.

Eviction Advisor

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?).

CachesExplained / Eviction

Of these, using SoftReference as a reference seems to be LRU-like.

For the time being

I think it's a good idea to keep this information as a starting point.

Recommended Posts

Java cache library and Eviction Policy
Java and JavaScript
XXE and Java
Java coverage measurement library
Use fast Mapping library MapStruct with Lombok and Java 11
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
Java bidirectional map library
timedatectl and Java TimeZone
[Java] Branch and repeat
Java CSV library "opencsv"
Java tree structure library
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Challenges and alternative research of Java serial communication library RXTX
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
[Design pattern] Java core library
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters