forked from huangzworks/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56d4f9c
commit bc3043e
Showing
20 changed files
with
303 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,42 @@ GETSET | |
|
||
**GETSET key value** | ||
|
||
将给定\ ``key``\ 的值设为\ ``value``\ ,并返回\ ``key``\ 的旧值。 | ||
将给定 ``key`` 的值设为 ``value`` ,并返回 ``key`` 的旧值(old value)。 | ||
|
||
当\ ``key``\ 存在但不是字符串类型时,返回一个错误。 | ||
当 ``key`` 存在但不是字符串类型时,返回一个错误。 | ||
|
||
**可用版本:** | ||
>= 1.0.0 | ||
|
||
**时间复杂度:** | ||
O(1) | ||
|
||
**返回值:** | ||
| 返回给定\ ``key``\ 的旧值(old value)。 | ||
| 当\ ``key``\ 没有旧值时,返回\ ``nil``\ 。 | ||
| 返回给定 ``key`` 的旧值。 | ||
| 当 ``key`` 没有旧值时,也即是, ``key`` 不存在时,返回 ``nil`` 。 | ||
:: | ||
|
||
redis> EXISTS mail | ||
(integer) 0 | ||
|
||
redis> GETSET mail [email protected] # 因为 mail 之前不存在,没有旧值,返回 nil | ||
redis> GETSET db mongodb # 没有旧值,返回 nil | ||
(nil) | ||
|
||
redis> GETSET mail [email protected] # mail 被更新,旧值被返回 | ||
"[email protected]" | ||
redis> GET db | ||
"mongodb" | ||
|
||
redis> GETSET db redis # 返回旧值 mongodb | ||
"mongodb" | ||
|
||
redis> GET db | ||
"redis" | ||
|
||
模式 | ||
-------- | ||
|
||
\ `GETSET`_\ 可以和\ `INCR`_\ 组合使用,实现一个有原子性(atomic)复位操作的计数器(counter)。 | ||
`GETSET`_ 可以和 `INCR`_ 组合使用,实现一个有原子性(atomic)复位操作的计数器(counter)。 | ||
|
||
举例来说,每次当某个事件发生时,进程可能对一个名为\ ``mycount``\ 的\ ``key``\ 调用\ `INCR`_\ 操作,通常我们还要在一个原子时间内同时完成获得计数器的值和将计数器值复位为\ ``0``\ 两个操作。 | ||
举例来说,每次当某个事件发生时,进程可能对一个名为 ``mycount`` 的 ``key`` 调用 `INCR`_ 操作,通常我们还要在一个原子时间内同时完成获得计数器的值和将计数器值复位为 ``0`` 两个操作。 | ||
|
||
可以用命令\ ``GETSET mycounter 0``\ 来实现这一目标。 | ||
可以用命令 ``GETSET mycounter 0`` 来实现这一目标。 | ||
|
||
:: | ||
|
@@ -44,7 +50,5 @@ GETSET | |
redis> GETSET mycount 0 # 一个原子内完成 GET mycount 和 SET mycount 0 操作 | ||
"11" | ||
|
||
redis> GET mycount | ||
redis> GET mycount # 计数器被重置 | ||
"0" | ||
|
||
|
Oops, something went wrong.