我正在研究Redis
与文档有关的问题
以下是官方Redis文档中有关到期的说明:
https://redis.io/commands/expire
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.
在我读完之后,我对上面的25% of keys
提出了一个问题
keys
中的25% of keys
含义究竟是什么?
keys
是Redis中的所有键吗?或上面描述中的20 random keys
?
我认为keys
看起来像是Redis中的所有键。
我在文档中找不到任何关于此的描述。
谢谢。
答案 0 :(得分:1)
默认情况下,Redis中的密钥没有生存时间(ttl)设置。该算法中的20个随机密钥从已设置显式ttl的所有密钥集合中采样。如果此示例中超过5个密钥已过期,则Redis中过期密钥的部分可能很高,因此算法会重复尝试将过期密钥的一部分驱动到低于阈值。
同样重要的是要注意,超出其ttl的密钥将在访问时自动过期(这意味着如果用户尝试请求它们)。因此,像这样的启发式算法只需要确保Redis不会堆积一堆过期的密钥,同时确保Redis不会花太多资源来清理过期的密钥。
查看源代码: https://github.com/antirez/redis/blob/unstable/src/expire.c