1、消极方法:每次访问key时判断key是否已经过期;
2、积极方法;周期性的从设置了过期时间的key中选择一部分的key进行删除
a、随机测试20个带有timeout信息的key
b、如果超过25%的key被删除,则重复执行整个流程
以下是redis官网原文
Specifically this is what Redis does 10 times per second:
- Test 20 random keys from the set of keys with an associated expire.
- Delete all the keys found expired.
- If more than 25% of keys were expired, start again from step 1.
官网地址:https://redis.io/commands/expire