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