Generic
EXPIRE
Sets a timeout on key. The key will automatically be deleted.
Arguments
keybodystrrequired
The key to set the timeout on.
secondsbodyint | datetime.timedeltarequired
The timeout in seconds as int or datetime.timedelta object
nxbodybool
Set expiry only when the key has no expiry
xxbodybool
Set expiry only when the key has an existing expiry
gtbodybool
Set expiry only when the new expiry is greater than current one
ltbodybool
Set expiry only when the new expiry is less than current one
Response
bool
True if the timeout was set
Example
# With secondsredis.set("mykey", "Hello")redis.expire("mykey", 5)assert redis.get("mykey") == "Hello"time.sleep(5)assert redis.get("mykey") is None# With a timedeltaredis.set("mykey", "Hello")redis.expire("mykey", datetime.timedelta(seconds=5))