redis文档翻译_key设置过期时间
Available since 1.0.0. ? ?使用開始版本號(hào)1.01
Time complexity:?O(1) ?時(shí)間復(fù)雜度O(1)
出處:http://blog.csdn.net/column/details/redisbanli.html
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上設(shè)置一個(gè)超時(shí)時(shí)間。
這個(gè)時(shí)間期滿后,key會(huì)自己主動(dòng)被刪除。key關(guān)聯(lián)一個(gè)超時(shí)時(shí)間,在Redis術(shù)語中叫volatile?(易揮發(fā)的)
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.
The timeout can also be cleared, turning the key back into a persistent key, using thePERSIST?command.
也能夠使用PERSIST命令將有過期時(shí)間的key轉(zhuǎn)換成永久的key。 If a key is renamed with?RENAME, the associated time to live is transferred to the new key name.
假設(shè)key被RENAME重命令,那么關(guān)聯(lián)的時(shí)間將被轉(zhuǎn)移到這個(gè)新的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.
假設(shè)一個(gè)key被RENAME命令覆蓋,比如已經(jīng)存在了一個(gè)Key_A,使用命令RENAME Key_B Key_A(將key_B重名為KEY_A),無論原來的Key_A有沒有關(guān)聯(lián)超時(shí)時(shí)間,新的Key_A將繼承Key_B的全部特性。
Refreshing expires ?刷新到期
It is possible to callEXPIREusing 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又一次為已經(jīng)存在的超時(shí)時(shí)間的key設(shè)置新的超時(shí)時(shí)間。
假如這樣做了。關(guān)聯(lián)這個(gè)key的超時(shí)時(shí)間將被更新成新設(shè)置的值。
在很多應(yīng)用很實(shí)用。比例如以下一節(jié)的樣例。
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版本號(hào)之前,使用命令改變key超時(shí)時(shí)間設(shè)置。這個(gè)值將明白影響key的刪除。
由于在那時(shí)固定復(fù)制層的限制,這樣的語義是必要的。
Return value ?返回值
Integer reply, specifically:特別的Iteger答復(fù)- 1?if the timeout was set. ? ?1代表設(shè)置了超時(shí)限制
- 0?if?key?does not exist or the timeout could not be set. 0表示key不存在或者key沒有設(shè)置超時(shí)時(shí)間。
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導(dǎo)航
Imagine you have a web service and you are interested in the latest N pagesrecently?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 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.想象一下。你有一個(gè)web服務(wù),而且你對(duì)你的用戶最后訪問的N個(gè)網(wǎng)頁非常感興趣,使得通過前面的每一個(gè)相鄰頁面訪問的運(yùn)行沒有超過60秒。從概念上講你可能覺得。頁面瀏覽量大的就是你的用戶設(shè)置頁面的導(dǎo)航會(huì)話,那么可能包括他或她眼下正在尋找關(guān)于產(chǎn)品種類的有趣信息,因此你就能夠推薦相關(guān)的產(chǎn)品。 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中你能夠使用一個(gè)簡(jiǎn)單的模式實(shí)現(xiàn):每次用戶訪問網(wǎng)頁就運(yùn)行以下的命令: MULTI RPUSH pagewviews.user:<userid> http://..... EXPIRE pagewviews.user:<userid> 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.
假設(shè)用戶閑置網(wǎng)頁超過60秒,key將要被刪除而且僅僅有興許訪問不同的網(wǎng)頁少于60秒才會(huì)被又一次記錄。
This pattern is easily modified to use counters using?INCR?instead of lists usingRPUSH.
這個(gè)模式非常easy地使用了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 theDELcommand.一般key創(chuàng)建沒關(guān)聯(lián)過期時(shí)間。這樣的key將永遠(yuǎn)存在,除非被用戶明白地刪除,比如用戶使用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命令族能去關(guān)聯(lián)給出給出key,有額外的內(nèi)存開銷。
當(dāng)一個(gè)key有設(shè)置過期時(shí)間,Redis確保當(dāng)過期時(shí)間過去時(shí)刪除這個(gè)key。
The key time to live can be updated or entirely removed using the?EXPIRE?andPERSIST?command (or other strictly related commands).
key的過期時(shí)間被更新或者刪除。使用命令EXPIRE 和PERSIST命令實(shí)現(xiàn)。Expire accuracy ?到期時(shí)間精確性
In Redis 2.4 the expire might not be pin-point accurate, and it could be between zero to one seconds out.
在2.4版本號(hào)之前,到期時(shí)間可能不太精確,可能會(huì)有0到1秒的誤差。
Since Redis 2.6 the expire error is from 0 to 1 milliseconds.
從2.6版本號(hào)之后誤差在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的過期時(shí)間信息是使用Unix的時(shí)間戳保存的(從2.6開始使用毫米級(jí)別)。
意思是即使Redis實(shí)例沒有執(zhí)行時(shí)間也是在流失的。
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).
為了使到期時(shí)間工作穩(wěn)定,必須保證計(jì)算機(jī)時(shí)間穩(wěn)定。
假設(shè)你在兩個(gè)計(jì)算機(jī)之間移動(dòng)RDB文件的時(shí)間延遲非常大。那么非常多有趣是的事情可能就會(huì)發(fā)生(比方全部設(shè)置到期時(shí)間的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總是在檢測(cè)計(jì)算機(jī)時(shí)間的,以至于假設(shè)你設(shè)置一個(gè)key的存活為1000秒,然后你設(shè)置你的計(jì)算機(jī)時(shí)間為2000秒之后的時(shí)間,那么這個(gè)key就會(huì)立馬過期。并且已經(jīng)過期1000秒了。
How Redis expires keys Redis的key是怎樣過期的
Redis keys are expired in two ways: a passive way, and an active way.
Redis key的過期方式有兩種:被動(dòng)過期和主動(dòng)過期
A key is actively expired simply when some client tries to access it, and the key is found to be timed out.
主動(dòng)方式過期比較簡(jiǎn)單。當(dāng)有client去訪問這個(gè)key時(shí),找到這個(gè)key而且使之超時(shí)。 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.當(dāng)然這還不夠,由于有些key可能在設(shè)置過期時(shí)間之后,就一直都沒有被訪問。
這些key不管怎樣都應(yīng)該被過期的,因此Redis會(huì)定期隨機(jī)抽取設(shè)置有過期時(shí)間的key進(jìn)行檢查。過期的key過期將被從 key空間刪除掉。
Specifically this is what Redis does 10 times per second:Redis每秒進(jìn)行10次檢測(cè): 1:Test 20 random keys from the set of keys with an associated expire. 隨機(jī)抽取設(shè)置有過期時(shí)間中的20個(gè)key。 2:Delete all the keys found expired. 刪除已經(jīng)過期的key。 3:If more than 25% of keys were expired, start again from step 1. 假設(shè)有超過25%的key被超時(shí)。那么開始步驟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%
這個(gè)概率算法是不重要的。主體上就是如果我們的樣品代表了整個(gè)key空間。而且繼續(xù)使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.
意思是不論什么時(shí)刻,給出已經(jīng)過期的最大數(shù)量key,這些過期key使用的最大內(nèi)存等于每秒最大數(shù)量寫操作除以4倍。
How expires are handled in the replication link and AOF file
在AOF文件和復(fù)制連接中過期是怎樣處理的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.
為了獲得不犧牲一致性的正確做法,當(dāng)一個(gè)key過期時(shí),DEL操作將在AOF文件和全部的 slaves中同步。這樣的方式中,過期在master實(shí)例集中處理,而且避免產(chǎn)生改變一致性錯(cuò)誤。
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.
然而,當(dāng)slaves連接到master上時(shí)。就不會(huì)獨(dú)立地使key過期(可是會(huì)等待master DEL的到來),他們將使用全部在數(shù)據(jù)設(shè)置中有過期狀態(tài)的key,因此當(dāng)一個(gè)slave被推選成為master時(shí),它將開始獨(dú)立地去過期key,充當(dāng)maste的角色。出處:http://blog.csdn.net/column/details/redisbanli.html
轉(zhuǎn)載于:https://www.cnblogs.com/gcczhongduan/p/5092564.html
總結(jié)
以上是生活随笔為你收集整理的redis文档翻译_key设置过期时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】 Linux内核中读写文件数据的方
- 下一篇: React-native键盘遮挡输入框问