#redis 过期key event订阅

https://redis.io/docs/manual/keyspace-notifications/#timing-of-expired-events

#就是非实时,可能晚很多。不建议用。只有background thread 扫到它,Delete时才会触发

Timing of expired events Keys with a time to live associated are expired by Redis in two ways:

When the key is accessed by a command and is found to be expired. Via a background system that looks for expired keys in the background, incrementally, in order to be able to also collect keys that are never accessed. The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero.

If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.

Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero.


#评论

#评论 1 · 2023-02-24T06:50:53.760000Z

https://github.com/tzongw/redis-timer redis的 一个插件

#评论 2 · 2023-02-24T06:53:31.370000Z

Redis 自动过期的实现方式是:定时任务离线扫描并删除部分过期键;在访问键时惰性检查是否过期并删除过期键。也就是说 Redis 中有一些键已经过期却未被删除,自然也不会发送通知。实验表明过期通知晚于设定时间数分钟发出的情况也十分常见(见参考资料:请勿过度依赖Redis的过期监听)。

#评论 3 · 2023-02-24T06:53:49.639000Z

二是键空间通知采用的是发送即忘(fire and forget)策略。如果因为连接断开或服务重启等原因错过了过期通知,那么服务恢复后也没有重试的机会了。

#评论 4 · 2023-02-24T07:01:03.546000Z

另一种。 redis zset->定时 把到期的zrangebyscore 到ready中,加上定时 retry 等 https://juejin.cn/post/7111939271757398023