暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Redis淘汰策略

产教Code 2020-04-12
544


Redis这里就不说了相信做开发的同学都清楚,这里不讲了。

下面我们说一下Redis的淘汰策略。






Redis淘汰策略主要分为8种



官方提示我们的淘汰机制为以下8种:


1、volatile-lru -> Evict using approximated LRU among the keys with an expire set.  

从已设置过期时间的数据集中挑选最近最少使用的数据淘汰。

2 、 allkeys-lru -> Evict any key using approximated LRU.

从已所有数据集中挑选最近最少使用的数据淘汰。

3 、 volatile-lfu -> Evict using approximated LFU among the keys with an expire set.  

从已设置过期时间的数据集挑选使用频率最低的数据淘汰。

4. allkeys-lfu -> Evict any key using approximated LFU.

从数据集中挑选使用频率最低的数据淘汰。

5 、volatile-random -> Remove a random key among the ones with an expire set.  从设置过期时间的数据中随机删除

6 、 allkeys-random -> Remove a random key, any key.

从所有数据中随机删除

7 、volatile-ttl -> Remove the key with the nearest expire time (minor TTL)

从已设置过期时间的数据集中挑选将要过期的数据淘汰。

8、noeviction -> Don't evict anything, just return an error on write operations.

禁止驱逐数据,这也是默认策略。意思是当内存不足以容纳新入数据时,新写入操作就会报错,请求可以继续进行,线上任务也不能持续进行,采用no-enviction策略可以保证数据不被丢失。


如何开启:

# In short... if you have replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
# 配置最大内存,64位机器默认不限制,32位机器默认最大3G maxmemory 100mb当redis内存达到100mb的时候触发
# 淘汰机制
maxmemory 100mb
# Note: with any of the above policies, Redis will return an error on write
# operations, when there are no suitable keys for eviction.
#
# At the date of writing these commands are: set setnx setex append
# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
# getset mset msetnx exec sort
#
# The default is:
# 配置淘汰策略默认为(不淘汰数据)
maxmemory-policy volatile-ttl



文章转载自产教Code,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论