Redis TTL实现机制。

参考文章链接 https://redis.io/commands/expire

1.redis有两种key的失效机制

1.被动失效 。

A key is passively expired simply when some client tries to access it, and the key is found to be timed out.

当客户端去获取key的时候,去判断key是否失效。

2.主动失效

Of course this is not enough as there are expired keys that will never be accessed again. These keys should be expired anyway, so periodically Redis tests a few keys at random among keys with an expire set. All the keys that are already expired are deleted from the keyspace.

Specifically this is what Redis does 10 times per second:

  1. Test 20 random keys from the set of keys with an associated expire.
  2. Delete all the keys found expired.
  3. If more than 25% of keys were expired, start again from step 1.

redis有一个定时任务每秒执行10次。

第一步,从有失效机制的key中随机取出20个key。

第二步,删除已经过期的key。

第三部,判断下是否超过1/4的key已经失效了,如果没有执行步骤第一步。

这样就可以保证在任何时间,过期的key占用的内存空间的最大值 是 每秒写操作 key/4.

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值