之前其实写过redis的过期的一些原理,这次次要是记录下,一些应用上的概念,次要是redis应用的过期策略是懒过期和定时革除,懒过期的其实比较简单,即是在key被拜访的时候会顺带着判断下这个key是否已过期了,如果曾经过期了,就不返回了,然而这种策略有个破绽是如果有些key之后始终不会被拜访了,就等于沉在池底了,所以须要有一个定时的清理机制,去从设置了过期的key池子(expires)里随机地捞key,具体的策略咱们看下官网的解释

  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.

从池子里随机获取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过期策略温习