EXPIRE
Introduction and Use Case(s)
The EXPIRE command in Redis sets a timeout on a specified key. When the timeout expires, the key will be automatically deleted. This is commonly used in scenarios where data should only be available for a limited period, such as caching, session management, or temporary data storage.
Syntax
EXPIRE key seconds
Parameter Explanations
key: The name of the key you want to set an expiration on.seconds: The time to live (TTL) for the key, specified in seconds. After this period, the key is automatically removed.
Return Values
The EXPIRE command returns an integer:
1if the timeout was successfully set.0if the key does not exist or the timeout could not be set.
Code Examples
dragonfly> SET mykey "Hello""OK"dragonfly> EXPIRE mykey 10(integer) 1dragonfly> TTL mykey(integer) 10dragonfly> EXISTS mykey(integer) 1// Wait for more than 10 secondsdragonfly> EXISTS mykey(integer) 0
Best Practices
- Use the
EXPIREcommand in conjunction withSETfor creating keys that should automatically expire. - Combine
EXPIREwithPERSISTto remove the expiration from a key if needed.
Common Mistakes
- Setting an expiration on non-existent keys results in a return value of
0, which can lead to unexpected behavior if not checked properly. - Confusing the units:
EXPIREuses seconds, whereasPEXPIREuses milliseconds.
FAQs
What happens if I set a new expiry on a key that already has an expiry?
Setting a new expiration time on a key overrides the existing expiration time.
Can I remove the expiration from a key?
Yes, use the PERSIST command to remove the expiration from a key.
Will EXPIRE work on all data types in Redis?
EXPIRE works on any type of key, including strings, lists, sets, hashes, etc.