文章目录
过期删除策略和内存淘汰策略
详细介绍请参考博客“redis过期删除策略和内存淘汰策略”
内存淘汰策略
为了节省内存,redis并没有采用传统的方法实现LRU和LFU而是基于随机采样的方式,近似实现LRU和LFU,并引入淘汰池进行优化。接下来详细看看是如何淘汰池进行优化的。具体实现在"evict.c"文件中的函数"evictionPoolPopulate"。
/* This is a helper function for performEvictions(), it is used in order
* to populate the evictionPool with a few entries every time we want to
* expire a key. Keys with idle time bigger than one of the current
* keys are added. Keys are always added if there are free entries.
*
* We insert keys on place in ascending order, so keys with the smaller
* idle time are on the left, and keys with the higher idle time on the
* right. */
这个函数是"performEvictions()"的辅助函数。每当想要过期一些key时该函数被用来向淘汰池填充一些数据。
当淘汰池未满时,keys总是被添加;反之的话,添加具有更大idle time的keys。
淘汰池按照idle time升序排序,即较小idle time的key存储在淘汰池的左边,较大idle time的key存储在淘汰池的右边。
/* When an LFU policy is used instead, a reverse frequency indication is used
* instead of the idle time, so that we still evict by larger value (larger
* inverse frequency means to evict keys with the least frequent accesses).*/
当使用LFU策略时,用反向频率reverse frequency代替idle time。
按照reverse frequency升序排序,较大的inverse frequency意味着keys具有较小的lfu值即least frequent accesses。