面试题之redis的过期时间原理

1、消极方法:每次访问key时判断key是否已经过期;

2、积极方法;周期性的从设置了过期时间的key中选择一部分的key进行删除

  a、随机测试20个带有timeout信息的key

  b、如果超过25%的key被删除,则重复执行整个流程

以下是redis官网原文

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.

官网地址:https://redis.io/commands/expire

原文地址:https://www.cnblogs.com/hujinshui/p/10226989.html