欢迎投稿

今日深度:

redis文档翻译_key设置过期时间

redis文档翻译_key设置过期时间



Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.
在key上设置一个超时时间。这个时间期满后,key会自动被删除。key关联一个超时时间,在Redis术语中叫volatile (易挥发的)


The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.
这个超时时间仅仅在使用 DEL命令或者使用SET或者GETSET命名重写时被清除。所有操作,从概念上在key没有替换,改变存储的值将与一个新的超时时间关联。例如,使用INCR增量操作一个值,使用LPUSH为list增加新值,或者使用HEST设置hash字段的新值,所有的操作重新关联一个超时时间。
The timeout can also be cleared, turning the key back into a persistent key, using thePERSIST command.
也可以使用PERSIST命令将有过期时间的key转换成永久的key。 If a key is renamed with RENAME, the associated time to live is transferred to the new key name.
如果key被RENAME重命令,那么关联的时间将被转移到这个新的key名字上。 If a key is overwritten by RENAME, like in the case of an existing key Key_A that is overwritten by a call like RENAME Key_B Key_A, it does not matter if the original Key_Ahad a timeout associated or not, the new key Key_A will inherit all the characteristics of Key_B.
如果一个key被RENAME命令覆盖,例如已经存在了一个Key_A,使用命令RENAME Key_B Key_A(将key_B重名为KEY_A),不管原来的Key_A有没有关联超时时间,新的Key_A将继承Key_B的所有特性。

 

Refreshing expires 刷新到期

It is possible to call EXPIRE using as argument a key that already has an existing expire set. In this case the time to live of a key is updated to the new value. There are many useful applications for this, an example is documented in the Navigation session pattern section below.
可以使用命令EXPIRE重新为已经存在的超时时间的key设置新的超时时间。假如这样做了,关联这个key的超时时间将被更新成新设置的值。在许多应用非常有用,例如下一节的例子。

Differences in Redis prior 2.1.3在Redis的2.1.3之前的区别

In Redis versions prior 2.1.3 altering a key with an expire set using a command altering its value had the effect of removing the key entirely. This semantics was needed because of limitations in the replication layer that are now fixed.
在Redis2.1.3版本之前,使用命令改变key超时时间设置,这个值将明确影响key的删除。因为在那时固定复制层的限制,这种语义是必要的。

Return value 返回值

Integer reply, specifically:特别的Iteger答复
1 if the timeout was set. 1代表设置了超时限制
0 if key does not exist or the timeout could not be set. 0表示key不存在或者key没有设置超时时间。

Examples

redis> SET mykey "Hello"
OK
redis> EXPIRE mykey 10
(integer) 1
redis> TTL mykey
(integer) 10
redis> SET mykey "Hello World"
OK
redis> TTL mykey
(integer) -1
redis>

Pattern: Navigation session 图像session导航

Imagine you have a web service and you are interested in the latest N pages recently visited by your users, such that each adjacent page view was not performed more than 60 seconds after the previous. Conceptually you may think at this set of page views as a Navigation session if your user, that may contain interesting information about what kind of products he or she is looking for currently, so that you can recommend related products.
想象一下,你有一个web服务,并且你对你的用户最后访问的N个网页很感兴趣,使得通过前面的每个相邻页面访问的执行没有超过60秒。从概念上讲你可能认为,页面浏览量大的就是你的用户设置页面的导航会话,那么可能包含他或她目前正在寻找关于产品种类的有趣信息,因此你就可以推荐相关的产品。 You can easily model this pattern in Redis using the following strategy: every time the user does a page view you call the following commands:
在Redis中你可以使用一个简单的模式实现:每次用户访问网页就执行下面的命令:
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 algorithm, basically the assumption is that our sample is representative of the whole key space, and we continue to expire until the percentage of keys that are likely to be expired is under 25%
这个概率算法是不重要的,主体上就是假设我们的样品代表了整个key空间,并且继续使key过期,直到有25%以上的key过期,然后开始第一步。
This means that at any given moment the maximum amount of keys already expired that are using memory is at max equal to max amount of write operations per second divided by 4.
意思是任何时刻,给出已经过期的最大数量key,这些过期key使用的最大内存等于每秒最大数量写操作除以4倍。

How expires are handled in the replication link and AOF file

在AOF文件和复制连接中过期是如何处理的

In order to obtain a correct behavior without sacrificing consistency, when a key expires, a DEL operation is synthesized in both the AOF file and gains all the attached slaves. This way the expiration process is centralized in the master instance, and there is no chance of consistency errors.

为了获得不牺牲一致性的正确做法,当一个key过期时,DEL操作将在AOF文件和所有的 slaves中同步。这种方式中,过期在master实例集中处理,并且避免产生改变一致性错误。

However while the slaves connected to a master will not expire keys independently (but will wait for the DEL coming from the master), they'll still take the full state of the expires existing in the dataset, so when a slave is elected to a master it will be able to expire the keys independently, fully acting as a master.

然而,当slaves连接到master上时,就不会独立地使key过期(但是会等待master DEL的到来),他们将使用所有在数据设置中有过期状态的key,因此当一个slave被推选成为master时,它将开始独立地去过期key,充当maste的角色。

www.htsjk.Com true http://www.htsjk.com/DB2/20454.html NewsArticle redis文档翻译_key设置过期时间 Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. 在key上设置一个超时...
评论暂时关闭