In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to set the Key of expiration time in Redis? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Commands such as DEL/SET/GETSET will clear the expiration time
When you use commands such as DEL, SET, GETSET, etc. that override the key corresponding value to operate a key with the expiration time set, it will cause the expiration time of the corresponding key to be cleared.
/ / set the expiration time of mykey to 300s 127.0.0.1 OK > set mykey hello ex 300 OK / / View the expiration time 127.0.0.1 OK > ttl mykey (integer) 294 / / overwrite the content of mykey with set command 127.0.0.1 integer 6379 > set mykey olleh OK / / Expiration time is cleared 127.0.0.1integer 6379 > ttl mykey (integer)-1
2. Commands such as INCR/LPUSH/HSET will not clear the expiration time.
Using INCR/LPUSH/HSET, which only modifies the value of one key, rather than overwriting the entire value, does not clear the expiration time of the key.
INCR:
/ / set the expiration time of incr_key as 300s 127.0.0.1 OK 6379 > set incr_key 1 ex 300 OK 127.0.0.1 OK 6379 > ttl incr_key (integer) 291 / / perform self-increment operation 127.0.0.1 OK 6379 > incr incr_key (integer) 2 127.0.0.1 ex 6379 > get incr_key "2" / / query the expiration time, and it is found that the expiration time has not been cleared 127.0.0.1 OK 6379 > ttl incr_key (integer) 277
LPUSH:
/ / add a key of type list And add a value of 1 127.0.0.1list 6379 > LPUSH list 1 (integer) 1 / / set the expiration time of 300s for list 127.0.0.1list 6379 > expire list 300 (integer) 1 / / View the expiration time 127.0.0.16379 > ttl list (integer) 292 / add a value of 2127.0.0.16379 > lpush list 2 (integer) 2 / View all values of list Lrange list 0 11) "2" 2) "1" / / you can see that adding a value to the list does not clear the expiration time 127.0.0.1 ttl list 6379 > ttl list (integer) 252
3. The PERSIST command clears the expiration time
When you use the PERSIST command to convert a key with an expiration time set to a persistent key, the expiration time is also cleared.
127.0.0.1 set persist_key ex OK 127.0.0.1 ttl persist_key 6379 > ttl persist_key (integer) 296 / / 127.0.0.1 ttl persist_key 6379 > persist persist_key (integer) 1 / / Expiration time is cleared 127.0.0.1 ttl persist_key (integer)-1
4. Using the RENAME command, the expiration time of the old key will be transferred to the new key
When renaming KEY_A to KEY_B using the RENAME KEY_A KEY_B command, the new key KEY_B will inherit all the features of KEY_A, regardless of whether KEY_B has set the expiration time or not.
/ / set the expiration time of key_a to 300s 127.0.0.1 OK / / set the expiration time of key_b to 600s 127.0.0.1 key_b 6379 > set key_b value_b ex 600 OK 127.0.1 set key_b value_b ex 6379 > ttl key_a (integer) 279 127.0.1 key_b 6379 > ttl key_b (integer) 591 / / rename key_a to key_b 127.0.0.1 : 6379 > rename key_a key_b OK / / the new key_b inherits the expiration time of key_a 127.0.0.1 key_a 6379 > ttl key_b (integer)
The space here is limited, so I will not rename key_a to key_b one by one. You can try to set the expiration time in key_a and not in key_b on your own computer.
5. Using EXPIRE/PEXPIRE to set the expiration time to a negative number or EXPIREAT/PEXPIREAT to set the expiration timestamp to the past time will cause the key to be deleted
EXPIRE:
127.0.0.1 set key_1 value_1 OK 6379 > get key_1 "value_1" / / set the expiration time to-1 127.0.0.1 integer 6379 > expire key_1-1 (integer) 1 / / found that key was deleted 127.0.0.1integer 6379 > get key_1 (nil)
EXPIREAT:
127.0.0.1 set key_2 value_2 OK 6379 > get key_2 "value_2" / / the timestamp set is the past time 127.0.0.1 value_2 6379 > expireat key_2 10000 (integer) 1 / / key is deleted 127.0.0.1 integer 6379 > get key_2 (nil)
6. The EXPIRE command can update the expiration time
You can update the expiration time of a key that has its expiration time set by using the expire command.
/ / set the expiration time of key_1 as 100s 127.0.0.1 key_1 6379 > set key_1 value_1 ex 100 OK 127.0.0.1 OK 6379 > ttl key_1 (integer) 95 / / the expiration time of updated key_1 is 300s 127.0.1 integer integer 6379 > expire key_1 300 (integer) 1 127.0.0.1 OK 6379 > ttl key_1 (integer) 295
In versions below Redis2.1.3, using the expire command to update the expiration time of a key that has been set will fail. And when you use commands such as LPUSH/HSET to modify the value of a key that has an expiration time set, it will cause Redis to delete the key.
Expiration Policy of Redis
Have you ever thought about the question, if there are a lot of key in Redis, how can we efficiently find out the expired key and delete it? is it to traverse every key? If a large number of key expires in the same period, will Redis cause stutters in read and write instructions because it has been dealing with expiration events?
To explain here, Redis is single-threaded, so some time-consuming operations can lead to Redis stutters, such as using the keys * command to list all key when the amount of Redi data is particularly large.
In fact, Redis uses a combination of lazy deletions and periodic deletions to deal with expired key.
Lazy deletion
Lazy deletion means that when the client accesses the key, redis will check the expiration time of the key and delete it immediately if it expires.
This approach seems perfect, checking the expiration time of key during access does not take up too much extra CPU resources. But if a key has expired, if it has not been accessed for a long time, then the key will remain in memory all the time, seriously consuming memory resources.
Delete periodically
The principle of periodic deletion is that Redis will put all the key with expiration time set in a dictionary, and then check the expiration time and delete the expired key from some random key from the dictionary at regular intervals.
By default, Redis performs 10 expiration scans per second:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Random 20 key from expired dictionaries
Delete expired key of these 20
If more than 25% of the key expires, repeat the first step
At the same time, in order to ensure that there is no excessive cycle, Redis also sets an upper limit of scanning time, which will not exceed 25ms by default.
After reading the above, have you mastered the method of how to set the expiration time Key in Redis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.