In Redis, the SET
command sets a key to hold a given string value.
If the key already holds a value, it is overwritten with the new value. Also, any previous time to live associated with the key is discarded (assuming a successful SET
operation).
Syntax
The syntax goes like this:
SET key value [ NX | XX] [GET] [ EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
Here’s an explanation of the various options:
EX seconds | Sets the specified expire time, in seconds. |
PX millisecond | Sets the specified expire time, in milliseconds. |
EXAT timestamp-second | Sets the specified Unix time at which the key will expire, in seconds. |
PXAT timestamp-milliseconds | Sets the specified Unix time at which the key will expire, in milliseconds. |
NX | Only sets the key if it does not already exist. |
XX | Only sets the key if it already exists. |
KEEPTTL | Retains the time to live associated with the key. |
GET | Returns the old string stored at the key, or nil if the key did not exist. An error is returned and SET aborted if the value stored at key is not a string. |
See the Redis documentation for any changes to the above information.
Example
Here’s a basic example to demonstrate:
SET type "Dog"
Result:
OK
In this case, the SET
operation was successful, and so we got a simple string reply of OK
.
We can now check the value of the type
key with the GET
command:
GET type
Result:
"Dog"
The NX
Option
Here’s an example of trying to set another value to the same key when using the NX
option:
SET type "Cat" NX
Result:
(nil)
The NX
option prevents the key being set if it already exists. In our case, it already exists and so the result is a null reply, or (nil)
.
The XX
Option
Here’s an example of using the XX
option:
SET type "Cat" XX
Result:
OK
The XX
option prevents the key being set if it doesn’t already exist. In our case, it already exists and so the key is set.
Here’s what happens if we try to set key that doesn’t already exist:
SET country "Thailand" XX
Result:
(nil)
The key wasn’t set, because it doesn’t already exist.
The GET
Option
Here’s an example of using the GET
option:
SET type "Bird" GET
Result:
"Cat"
The GET
option returns the old value of the key. In our case, we had previously set the type
key to Cat
and so that’s what is returned.
Bear in mind that the key now holds the new value:
GET type
Result:
"Bird"
This option can be used as a replacement for the GETSET
command (which has been regarded as deprecated since Redis 6.2.0).
Set an Expiry Time
Here’s an example of using the EX
option to set an expiry time:
SET type "Horse" EX 5
Result:
OK
The EX
option allows us to set an expiry time, in seconds. In this case, I set the expiry time to 5 seconds.
Here’s what happens when I run the GET
command after waiting for more than 5 seconds:
GET type
Result:
(nil)
So the key no longer exists.
We can alternatively use the PX
option to specify the expiry time in milliseconds, or the EXAT
option to set the Unix time at which the key will expire, in seconds, or PXAT
for milliseconds.