查看Redis的默认设置的过期策略和内存淘汰机制
  TEZNKK3IfmPf 2023年11月15日 15 0

找到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种

在设置了过期时间的键空间中,优先移除最近未使用的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新引入的策略,根据自己的需要自行设置


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月19日   17   0   0 javarediskey
TEZNKK3IfmPf