Redis sets expiration time
In Redis, we can set an expiration time for the key to ensure that the key will be automatically deleted when the specified time arrives.
This mechanism is very useful for application scenarios where data is temporarily stored, such as caching, session management, etc.
1. Basic concepts
- Expiration time: Refers to the valid time when a key exists in Redis. After this time exceeds this time, Redis will automatically delete the key to free up memory.
- Time to survive (TTL): indicates the remaining survival time of the key, which can be obtained and set through commands.
2. Methods to set expiration time
Redis provides a variety of commands to set or modify the expiration time of a key. Here are some commonly used commands:
a. EXPIRE key seconds
Function: Set the expiration time for the specified key, in seconds.
Return value:
- If the expiration time is successfully set, return
1
。 - If the key does not exist, return
0
。
Example:
redis> EXPIRE mykey 60 (integer) 1
This command sets the keymykey
Expired after 60 seconds.
b. PXPIRE key milliseconds
Function: Sets the expiration time in milliseconds for the specified key.
Return value:
- If the expiration time is successfully set, return
1
。 - If the key does not exist, return
0
。
Example:
redis> PXPIRE mykey 5000 (integer) 1
This command sets the keymykey
Expires after 5000 milliseconds (i.e. 5 seconds).
c. EXPIREAT key timestamp
Function: Set a specific expiration time for the specified key, using a Unix timestamp (the number of seconds calculated from epoch).
Return value:
- If the expiration time is successfully set, return
1
。 - If the key does not exist, return
0
。
Example:
redis> EXPIREAT mykey 1623859200 (integer) 1
This command sets the keymykey
In Unix timestamp1623859200
(i.e. 00:00:00 UTC on June 14, 2021) expires.
d. PEXPIREAT key timestamp milliseconds
Function:andEXPIREAT
Similar, but timestamps use millisecond precision.
Return value:
- If the expiration time is successfully set, return
1
。 - If the key does not exist, return
0
。
Example:
redis> PEXPIREAT mykey 1623859200000 (integer) 1
This command sets the keymykey
In Unix timestamp1623859200000
(i.e. 00:00:00 UTC on June 14, 2021, millisecond accuracy) expired.
e. useSET
Command set expiration time
- Redis's
SET
The command supports specifying expiration time while setting key values. - Options available
EX
(in seconds) orPX
(in milliseconds) to achieve this.
Example:
redis> SET mykey "hello" EX 60 OK redis> SET mykey2 "world" PX 5000 OK
- The first command set key
mykey
The value of"hello"
, and expires after 60 seconds. - The second command setting key
mykey2
The value of"world"
, and expires after 5000 milliseconds (i.e. 5 seconds).
3. Obtain and modify TTL
a. TTL key
Function: Returns the remaining survival time (in seconds) of the specified key.
Return value:
- If the key exists and has a set expiration time, return the remaining seconds.
- If the key does not exist, return
-2
。 - If the key exists but the expiration time is not set, return
-1
。
Example:
redis> SET mykey "test" EX 60 OK redis> TTL mykey (integer) 58 redis> TTL nonexistkey (integer) -2 redis> TTL noexpirekey (integer) -1
b. PTTL key
Function:andTTL
Similar, but returns the remaining survival time in milliseconds.
Return value:
- If the key exists and there is a set expiration time, return the remaining number of milliseconds.
- If the key does not exist, return
-2
。 - If the key exists but the expiration time is not set, return
-1
。
Example:
redis> SET mykey "test" PX 5000 OK redis> PTTL mykey (integer) 4837
c. Modify TTL
- Can be passed
EXPIRE
,PXPIRE
,EXPIREAT
, orPEXPIREAT
The command resets the expiration time, thereby modifying the remaining key's survival time.
Example:
redis> SET mykey "test" EX 60 OK redis> TTL mykey (integer) 58 redis> EXPIRE mykey 30 (integer) 1 redis> TTL mykey (integer) 29
4. Delete policy for expired keys
Redis uses two strategies to handle expired keys:
a. Lazy Deletion
- An expired key is not deleted immediately, but is checked the next time the key is accessed. If you find that the key has expired, it will be deleted automatically.
- Advantages: Reduces memory resource consumption and CPU usage, as expired keys are not frequently scanned and deleted.
b. Active Deletion
- Redis regularly proactively scans the database for expired keys and deletes them. This strategy helps to avoid excessive expired keys piled up in memory.
- Advantages: It can free up memory space more timely and maintain high performance and stability.
5. Set expiration time in batches
When you need to set an expiration time for multiple keys, you can use Redis'sMULTI
andEXEC
Commands package multiple commands to execute, thereby achieving atomic operations.
Example:
redis> MULTI OK redis> EXPIRE key1 60 QUEUED redis> EXPIRE key2 30 QUEUED redis> EXEC 1) OK 2) OK
6. Things to note
-
Precision of expiration time:use
PX
andPEXPIREAT
When commanded, the time is in milliseconds, which can provide higher accuracy. -
Selection of timestamps: In use
EXPIREAT
andPEXPIREAT
When you are , you need to make sure that the provided Unix timestamp is correct and consistent with the current system time. - Memory and performance impact: Too much setting of expired keys may increase the load on Redis, especially in high concurrency. Therefore, it is recommended to plan the expiration time and TTL values reasonably to optimize resource usage.
Summarize
Through the above commands and strategies, you can flexibly set, obtain and modify the key's survival time (TTL) in Redis. This not only helps manage memory resources, but also improves the performance and stability of the application.
In practical applications, the appropriate expiration time and strategy should be selected according to the specific business needs to achieve the best results.
Related links:
- Redis official documentation - TTL
- Redis official documentation - EXPIRE
- Redis official documentation - PEXPIRE
With the above, you can have a comprehensive understanding of how to set and manage key survival times in Redis.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.