String
SET
Set a key to hold a string value.
Arguments
keybodystrrequired
The key
valuebodyTValuerequired
The value, if this is not a string, we will use JSON.stringify to convert it
to a string.
getbodybool
Instead of returning True, this will cause the command to return the old
value stored at key, or None when key did not exist.
exbodyint
Sets an expiration (in seconds) to the key.
pxbodyint
Sets an expiration (in milliseconds) to the key.
exatbodyint
Set the UNIX timestamp in seconds until the key expires.
pxatbodyint
Set the UNIX timestamp in milliseconds until the key expires.
keepttlbodybool
Keeps the old expiration if the key already exists.
nxbodybool
Only set the key if it does not already exist.
xxbodybool
Only set the key if it already exists.
Response
required
True if the key was set.
If get is specified, this will return the old value stored at key, or None when
the key did not exist.
Basic
assert redis.set("key", "value") == Trueassert redis.get("key") == "value"With nx and xx
# Only set the key if it does not already exist.assert redis.set("key", "value", nx=True) == False# Only set the key if it already exists.assert redis.set("key", "value", xx=True) == TrueWith expiration
# Set the key to expire in 10 seconds.assert redis.set("key", "value", ex=10) == True# Set the key to expire in 10000 milliseconds.assert redis.set("key", "value", px=10000) == TrueWith old value
# Get the old value stored at the key.assert redis.set("key", "new-value", get=True) == "old-value"