找到redis的配置文件,搜索The default is
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones with an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# Both LRU, LFU and volatile-ttl are implemented using approximated
# randomized algorithms.
#
# 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 noeviction
上述可知默认的淘汰策略为noeviction
而可供选择的淘汰策略有8种
volatile-lru
在设置了过期时间的键空间中,优先移除最近未使用的key
allkeys-lru
在所有主键空间中,优先移除最近未使用的key
volatile-lfu
在设置了过期时间的键空间中,将访问频率最少的键值对淘汰
allkeys-lfu
在所有主键空间中,将访问频率最少的键值对淘汰
volatile-random
在设置了过期时间的键空间中,随机移除某个key
allkeys-random
在所有主键空间中,随机移除某个key
volatile-ttl
从已设置过期时间的数据集中挑选将要过期的数据淘汰
noeviction
如果缓存数据超过了maxmemory限定值,并且客户端正在执行的命令(大部分的写入指令,但DEL和几个指令例外)会导致内存分配,则向客户端返回错误响应
其中volatile-lfu 和 allkeys-lfu是Redis 4.0新引入的策略,根据自己的需要自行设置