?
This document uses PHP Chinese website manual Release
GETSET key value
自1.0.0起可用。
時間復雜度: O(1)
以原子方式設置key
到value
,并返回存儲在舊值key
。key
存在但返回一個錯誤,但不包含字符串值。
GETSET 可以與 INCR 一起用于計數(shù)原子重置。例如:mycounter
每次某個事件發(fā)生時,一個進程可能會調(diào)用 INCR 來對付該鍵,但是我們需要不時地獲取該計數(shù)器的值并將其重置為零。這可以使用GETSET mycounter "0"
:
redis> INCR mycounter (integer) 1
redis> GETSET mycounter "0" "1"
redis> GET mycounter "0"
散裝字符串回復:舊值存儲在key
,或者nil
當key
不存在。
redis> SET mykey "Hello" "OK"
redis> GETSET mykey "World" "Hello"
redis> GET mykey "World"