设为首页 加入收藏

TOP

redis文档翻译_key设置过期时间(二)
2015-07-24 09:25:42 来源: 作者: 【 】 浏览:3
Tags:redis 文档 翻译 _key 设置 过期 时间
:每次用户访问网页就执行下面的命令:
MULTI
RPUSH pagewviews.user:
  
    http://.....
EXPIRE pagewviews.user:
   
     60 EXEC
   
  

If the user will be idle more than 60 seconds, the key will be deleted and only subsequent page views that have less than 60 seconds of difference will be recorded.

如果用户闲置网页超过60秒,key将要被删除并且只有后续访问不同的网页少于60秒才会被重新记录。

This pattern is easily modified to use counters using INCR instead of lists usingRPUSH.

这个模式很简单地使用了list的RPUSH代码自曾INCR。

Appendix: Redis expires 附件:Redis到期

?

Keys with an expire key的到期

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.
一般key创建没关联过期时间。这种key将永远存在,除非被用户明确地删除,例如用户使用DEL命令。

The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. When a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed.

EXPIRE命令族能去关联给出给出key,有额外的内存开销。当一个key有设置过期时间,Redis确保当过期时间过去时删除这个key。

The key time to live can be updated or entirely removed using the EXPIRE andPERSIST command (or other strictly related commands).

key的过期时间被更新或者删除,使用命令EXPIRE 和PERSIST命令实现。

Expire accuracy 到期时间精确性

In Redis 2.4 the expire might not be pin-point accurate, and it could be between zero to one seconds out.

在2.4版本之前,到期时间可能不太精确,可能会有0到1秒的误差。

Since Redis 2.6 the expire error is from 0 to 1 milliseconds.

从2.6版本之后误差在0到1毫米之间。

?

Expires and persistence 到期和持久

Keys expiring information is stored as absolute Unix timestamps (in milliseconds in case of Redis version 2.6 or greater). This means that the time is flowing even when the Redis instance is not active.
key的过期时间信息是使用Unix的时间戳保存的(从2.6开始使用毫米级别)。意思是即使Redis实例没有运行时间也是在流失的。
For expires to work well, the computer time must be taken stable. If you move an RDB file from two computers with a big desync in their clocks, funny things may happen (like all the keys loaded to be expired at loading time).
为了使到期时间工作稳定,必须保证计算机时间稳定。如果你在两个计算机之间移动RDB文件的时间延迟非常大,那么很多有趣是的事情可能就会发生(比如所有设置到期时间的key刚被加载就过期了)。
Even running instances will always check the computer clock, so for instance if you set a key with a time to live of 1000 seconds, and then set your computer time 2000 seconds in the future, the key will be expired immediately, instead of lasting for 1000 seconds.
甚至Redis总是在检测计算机时间的,以至于如果你设置一个key的存活为1000秒,然后你设置你的计算机时间为2000秒之后的时间,那么这个key就会立刻过期,而且已经过期1000秒了。

How Redis expires keys Redis的key是如何过期的

Redis keys are expired in two ways: a passive way, and an active way.

Redis key的过期方式有两种:被动过期和主动过期

A key is actively expired simply when some client tries to access it, and the key is found to be timed out.

主动方式过期比较简单,当有客户端去访问这个key时,找到这个key并且使之超时。 Of course this is not enough as there are expired keys that will never be accessed again. These keys should be expired anyway, so periodically Redis tests a few keys at random among keys with an expire set. All the keys that are already expired are deleted from the keyspace.
当然这还不够,因为有些key可能在设置过期时间之后,就一直都没有被访问。这些key无论如何都应该被过期的,因此Redis会定期随机抽取设置有过期时间的key进行检查。过期的key过期将被从 key空间删除掉。 Specifically this is what Redis does 10 times per second:
Redis每秒进行10次检测: 1:Test 20 random keys from the set of keys with an associated expire. 随机抽取设置有过期时间中的20个key。 2:Delete all the keys found expired. 删除已经过期的key。 3:If more than 25% of keys were expired, start again from step 1. 如果有超过25%的key被超时,那么开始步骤1。
This is a trivial probabilistic algorith
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇redis内存优化、持久化以及主从复.. 下一篇一起写redis脚本

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)