"Redis set command "
SET key value [EX seconds] [PX milliseconds] [nx| XX]
Associates a String valuevalueto akey.
Ifkeyanother value is already held, the SET will overwrite the old value, ignoring the type.
For a key with a time-to-live (TTL), the original TTL of this key is cleared when the SET command executes successfully on the key.
Optional parameters
Starting with the Redis 2.6.12 release, the behavior of the SET command can be modified by a series of parameters:
-
- ex second &NBSP: Set key expiration time is second sec. set key Value ex second Effect equivalent to setex key second value .
-
- px millisecond : Set key expiration time is millisecond milliseconds. set key Value px millisecond effect equivalent to psetex key millisecondvalue .
-
- NX: The key is set only if the key does not exist.SET key value NXthe effect is equal toSETNX key value.
-
- XX: The key is set only when the key already exists.
Because the SET command can be used to implement the effects of Setnx, Setex, and Psetex three commands with parameters, future Redis versions may be discarded and eventually removed from the three commands setnx, Setex, and Psetex.
return value:
The SET command is always returned before the Redis 2.6.12 Version.OK
Starting with the Redis 2.6.12 release, set returns when the set operation completes successfullyOK。 If you set theNXOrXX, the command returns an empty bulk reply (null Bulk Reply) because the condition is not met and the setup operation is not executed.
" usage mode "
CommandsSET resource-name anystring NX EX max-lock-timeare a simple way to implement locks in Redis.
The client executes the above command:
- If the server returnsOK, then the client obtains the lock.
- If the server returnsNIL, then the client acquires the lock failure and can retry later.
After the set expiration time arrives, the lock is automatically freed.
Reference: http://redisdoc.com/string/set.html
Redis set Command