LFU
Least Frequently Used。在数据访问随时间概率不变时,这种缓存的命中率是最高的。而且LFU的成本也是最高的。
LRU
依赖于“时间局部性”的一种替代LFU的缓存方案叫做LRU,LRU中最近被访问的数据项都会被插入缓存,而当缓存满时,最久未被访问的会被淘汰。相比于LFU,LRU的性能会更高,而且自动适应数据的时间局部性,并且可以应对数据的突发流量。但是在实际的场景中,LRU需要更大的缓存空间,才能和LFU达到相同的缓存命中率。
LRU的典型实现如下
对于访问的节点,移送到/建立在链表开头。容量不足的时候,淘汰尾指针对应的节点。
SLRU
SLRU cache is divided into two segments, a probationary segment and a protected segment. Lines in each segment are ordered from the most to the least recently accessed. Data from misses is added to the cache at the most recently accessed end of the probationary segment. Hits are removed from wherever they currently reside and added to the most recently accessed end of the protected segment. Lines in the protected segment have thus been accessed at least twice. The protected segment is finite, so migration of a line from the probationary segment to the protected segment may force the migration of the LRU line in the protected segment to the most recently used (MRU) end of the probationary segment, giving this line another chance to be accessed before being replaced. The size limit on the protected segment is an SLRU parameter that varies according to the I/O workload patterns. Whenever data must be discarded from the cache, lines are obtained from the LRU end of the probationary segment
SLRU被分为两个段,试用段和保护段。新数据会被加到试用段里。如果试用段或者保护段的数据再次被命中,那么数据会被加入到保护段的头部。保护段的大小是有限的。如果需要清除数据,那么数据会从保护段的末尾开始清除。
TinyLFU
大概的思路就是,新增一个新的元素时,判断使用该元素替换一个旧元素,是否能够提高缓存命中率。为了做到这一点,tinyLFU需要维护相当长的一段历史时期内数据项访问的频率统计信息。为了做到这一点,我们使用了CBF(计数布隆过滤器)。
Window-TinyLFU
实际上就是结合了LRU和LFU