Redis补充(内存淘汰策略)
2022-09-22 22:46:53
redis淘汰策略
这里面其实就只是分了两种:
- 包含过期的key
- 所有的key
每种key都有这么几种策略:
lru:最近最少使用原则
lfu:使用频率最低原则
ttl:如果设置过期时间,则从已经过期的时间中挑选要过期的
random:随机淘汰已经过期的
(1)volatile-lru:从已设置过期时间的数据集中挑选最近最少使用的数据淘汰。
(2)volatile-ttl:从已设置过期时间的数据集中挑选将要过期的数据淘汰。
(3)volatile-random:从已设置过期时间的数据集中任意选择数据淘汰。
(4)volatile-lfu:从已设置过期时间的数据集挑选使用频率最低的数据淘汰。
(5)allkeys-lru:从数据集中挑选最近最少使用的数据淘汰
(6)allkeys-lfu:从数据集中挑选使用频率最低的数据淘汰。
(7)allkeys-random:从数据集(server.db[i].dict)中任意选择数据淘汰
(8) no-enviction(驱逐):禁止驱逐数据,这也是默认策略。意思是当内存不足以容纳新入数据时,新写入操作就会报错,请求可以继续进行,线上任务也不能持续进行,采用no-enviction策略可以保证数据不被丢失。
这八种大体上可以分为4中,lru、lfu、random、ttl
这也是再配置文件里进行配置的,redis.conf相应内容如下:
1 | # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory |