Redis SET command

Source: Internet
Author: User



"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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.