Redis EXPIRE
Redis EXPIRE Command
Redis Expire command is used to set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.
- The timeout is cleared only when the key is removed using the DELcommand or overwritten using the SET or GETSET commands.
- The timeout can also be cleared, turning the key back into a persistent key, using the PERSIST command.
- If a key is renamed with RENAME, the associated time to live is transferred to the new key name.
Here are some common uses and scenarios for the "EXPIRE" command:
- Automatic Cleanup: Automatically delete keys after a specified period.
- Cache Management: Ensure cached data expires and is refreshed periodically.
- Session Management: Invalidate user sessions after inactivity.
- Temporary Data: Manage temporary tokens or one-time-use data with expiration.
- Rate Limiting: Control the lifespan of keys used for rate limiting.
Syntax:
Expire KEY_NAME TIME_IN_SECONDS
Available since
1.0.0.
Return Value
-
Integer value reply specifically:
- 1, if the timeout is set for the key.
- 0, if the key does not exist or timeout could not set.
Return Value Type
Integer
Example: Redis EXPIRE
First, create a key in redis and set some value in it.
127.0.0.1:6379> SET key "Apple" OK 127.0.0.1:6379> EXPIRE key 5 (integer) 1 127.0.0.1:6379> TTL key (integer) -2 127.0.0.1:6379> SET key "Banana" OK 127.0.0.1:6379> TTL key (integer) -1
In the above example time is set for the key Apple. After 5 minute key will expire automatically.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/redis/redis-expire-key.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics