程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> redis文檔翻譯_key設置過期時間

redis文檔翻譯_key設置過期時間

編輯:DB2教程

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的角色。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved