共计 1266 个字符,预计需要花费 4 分钟才能阅读完成。
之前其实写过 redis 的过期的一些原理,这次次要是记录下,一些应用上的概念,次要是 redis 应用的过期策略是懒过期和定时革除,懒过期的其实比较简单,即是在 key 被拜访的时候会顺带着判断下这个 key 是否已过期了,如果曾经过期了,就不返回了,然而这种策略有个破绽是如果有些 key 之后始终不会被拜访了,就等于沉在池底了,所以须要有一个定时的清理机制,去从设置了过期的 key 池子(expires)里随机地捞 key,具体的策略咱们看下官网的解释
- 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.
从池子里随机获取 20 个 key,将其中过期的 key 删掉,如果这其中有超过 25% 的 key 曾经过期了,那就再来一次,以此放弃过期的 key 不超过 25%(左右),并且这个定时策略能够在 redis 的配置文件
# Redis calls an internal function to perform many background tasks, like
# closing connections of clients in timeout, purging expired keys that are
# never requested, and so forth.
#
# Not all tasks are performed with the same frequency, but Redis checks for
# tasks to perform according to the specified "hz" value.
#
# By default "hz" is set to 10. Raising the value will use more CPU when
# Redis is idle, but at the same time will make Redis more responsive when
# there are many keys expiring at the same time, and timeouts may be
# handled with more precision.
#
# The range is between 1 and 500, however a value over 100 is usually not
# a good idea. Most users should use the default of 10 and raise this up to
# 100 only in environments where very low latency is required.
hz 10
能够配置这个 hz 的值,代表的含意是每秒的执行次数,默认是 10,其实也用了 hz 的广泛含意。有趣味能够看看之前写的一篇文章 redis 系列介绍七 - 过期策略
本文应用「署名 4.0 国内 (CC BY 4.0)」许可协定,欢送转载、或从新批改应用,但须要注明起源。署名 4.0 国内 (CC BY 4.0)
本文作者: Nicksxs
创立工夫: 2021-07-25
本文链接: redis 过期策略温习
正文完