key设置过期时间

Available since 1.0.0. 使用开始版本1.01

Time complexity:O(1) 时间复杂度O(1)

出处:

Set a timeout onkey. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to bein Redis terminology.

在key上设置一个超时时间。这个时间期满后,key会自动被删除。key关联一个超时时间,在Redis术语中叫volatile(易挥发的)

command or overwritten using theorcommands. This means that all the operations that conceptuallyalterthe 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 withINCR, pushing a new value into a list withLPUSH, or altering the field value of a hash withare all operations that will leave the timeout untouched.

这个超时时间仅仅在使用 DEL命令或者使用SET或者GETSET命名重写时被清除。所有操作,从概念上在key没有替换,改变存储的值将与一个新的超时时间关联。例如,使用INCR增量操作一个值,使用LPUSH为list增加新值,或者使用HEST设置hash字段的新值,所有的操作重新关联一个超时时间。

command.

也可以使用PERSIST命令将有过期时间的key转换成永久的key。

If a key is renamed withRENAME, the associated time to live is transferred to the new key name.

如果key被RENAME重命令,,那么关联的时间将被转移到这个新的key名字上。

If a key is overwritten byRENAME, like in the case of an existing keyKey_Athat is overwritten by a call likeRENAME Key_B Key_A, it does not matter if the originalKey_Ahad a timeout associated or not, the new keyKey_Awill inherit all the characteristics ofKey_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 callusing as argument a key that already has an existing expire set. In this case the time to live of a key isupdatedto the new value. There are many useful applications for this, an example is documented in theNavigation sessionpattern section below.

可以使用命令EXPIRE重新为已经存在的超时时间的key设置新的超时时间。假如这样做了,关联这个key的超时时间将被更新成新设置的值。在许多应用非常有用,例如下一节的例子。

Differences in Redis prior 2.1.3在Redis的2.1.3之前的区别In Redis versions prior2.1.3altering 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 返回值

, specifically:特别的Iteger答复

1if the timeout was set. 1代表设置了超时限制0ifkeydoes not exist or the timeout could not be set. 0表示key不存在或者key没有设置超时时间。

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

redis>

Pattern: Navigation session 图像session导航Imagine you have a web service and you are interested in the latest N pagesrecentlyvisited 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 aNavigation sessionif 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中你可以使用一个简单的模式实现:每次用户访问网页就执行下面的命令:

MULTIRPUSH pagewviews.user:<userid> EXPIRE pagewviews.user:<userid> 60EXEC

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 usinginstead of lists usingRPUSH.

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

也许叔本华是对的,人与人的距离太远会寂寞到寒冷,

key设置过期时间

相关文章:

你感兴趣的文章:

标签云: