Cache Eviction Policies
A cache is transient storage, meaning it’s short-lived. The small amount of cache limits the amount of data to store.
We need to track the data we store or remove over time. There are a number of algorithms that are used to manage the memory of a cache:
Least Recently Used (LRU)
The cache discards the least recently used data. It’s implemented using a timestamp for the last access.
If the data has not been used recently, we assume the chances of being called are less compared to others so removing it provides space for more recent data in the cache.
Least Frequently Used (LFU)
In this policy, we count the number of times a cache item is accessed. The one with the least use is discarded first. It’s assumed that the least used data are wasting space.
First In First Out (FIFO)
This policy works like a queue. The cache evicts data that is accessed first. It does not consider the number of times that data has been accessed before.
Last In First Out (LIFO)
This policy works like a stack(LIFO). The cache removes data that is recently added.
Random Selection
Here, the system randomly selects a data item from the cache memory to give space when necessary.