February 2018
Intermediate to advanced
382 pages
11h 33m
English
If there is already a value associated with the key, the SET command overwrites the value. Sometimes we don't want the value to be overwritten blindly if the key exists; one thing we can do is use the EXIST command to test the existence of the key before executing SET. However, Redis provides a command SETNX (short for SET if not exists), which can be used to set the value for a key, but only if the key does not exist, in an atomic operation. SETNX returns 1 if the key was successfully set, and returns 0 if the key already exists, so the old value will not be overwritten:
127.0.0.1:6379> SETNX "Lobster Palace" "437 Main St, Chicago, IL" (integer) 1 127.0.0.1:6379> SETNX "Extreme Pizza" "100 Broadway, New York, NY" (integer) ...
Read now
Unlock full access