?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
GETSET key value
自1.0.0起可用。
時(shí)間復(fù)雜度: O(1)
以原子方式設(shè)置key
到value
,并返回存儲(chǔ)在舊值key
。key
存在但返回一個(gè)錯(cuò)誤,但不包含字符串值。
GETSET 可以與 INCR 一起用于計(jì)數(shù)原子重置。例如:mycounter
每次某個(gè)事件發(fā)生時(shí),一個(gè)進(jìn)程可能會(huì)調(diào)用 INCR 來對付該鍵,但是我們需要不時(shí)地獲取該計(jì)數(shù)器的值并將其重置為零。這可以使用GETSET mycounter "0"
:
redis> INCR mycounter (integer) 1
redis> GETSET mycounter "0" "1"
redis> GET mycounter "0"
散裝字符串回復(fù):舊值存儲(chǔ)在key
,或者nil
當(dāng)key
不存在。
redis> SET mykey "Hello" "OK"
redis> GETSET mykey "World" "Hello"
redis> GET mykey "World"