redis的过期策略都有哪些?内存淘汰机制都有哪些

redis set key的时候,我们可以指定一个过期时间,时间到了以后,这个key就会失效,再去get 这个key的value的时候,就会获取到空值。redis是怎么清理过期的key呢?

定期删除+惰性删除

  • 定期删除
    redis默认经过每100ms就会检查设置了过期时间的key,如果有过期,就会清理掉。但是不是每个key都会去检查,只是会随机抽取部分key进行检查。因为如果每隔100ms就检查所有的key,CPU的负载将会非常高。
  • 惰性删除
    如果定期删除没有扫描到的key,下次再去get的时候,会先检查一次是否过期,如果过期,就会清理该key,并且返回空。这就是惰性清理

内存淘汰机制。

当放进redis里面的数据超出或即将超出了物理机器的内存,则会触发redis内存淘汰机制。看官方redis.conf里面有这么一段话:

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
# 
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key 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 expire at all, just return an error on write operations
# 
# 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-lru

说的很明白了,看不懂英文的自己翻译一下吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值