Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the validity period of key by redis

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to set the validity period of key by redis". In daily operation, I believe many people have doubts about how to set the validity period of key by redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to set the validity period of key by redis". Next, please follow the editor to study!

Redis sets the validity period of key Key command

Sets the expiration time of the key.

Query all current key

Expire key second: sets the expiration time of key

If the ttl key is displayed as-2, the change does not exist in the cache.

Display as-1 is persistent (will not fail)

If the failure time is set, then the remaining failure time will be displayed.

Thoroughly understand the issue of validity of redis

EXPIRE key seconds

Starting version: 1.0.0

Time complexity: O (1)

Return value: 1 if the expiration time is set successfully; 0 if the key does not exist or cannot be set.

Set the time to live for a given key, which is automatically deleted when the key expires (time to live is 0).

1. The expiration time follows key, regardless of the value.

In Redis, key with expiration time is called "volatile" (volatile). The expiration time can be removed by using the DEL command to delete the entire key, or it can be overridden (overwrite) by the SET and GETSET commands, which means that if a command simply alter the value of a key with expiration time instead of replacing it with a new key value, then the expiration time will not be changed. For example, performing INCR commands on a key, LPUSH commands on a list, or HSET commands on a hash table do not change the expiration time of the key itself.

two。 Set permanent validity period

Use the PERSIST command to clear the timeout and make it a permanent key.

The influence of 3.rename command on the validity period

If the key is modified by the RENAME command, the associated timeout will be transferred to the new key.

If the key is modified by the RENAME command, for example, the Key_A already exists, and then the RENAME Key_B Key_A command is called, whether the original Key_A is permanent or timed out, it will be overwritten by the validity status of the Key_B.

4. Refresh expiration time

EXPIRE a key that already has an expiration time, and its expiration time will be updated.

5. Expiration accuracy

In Redis 2.4 and previous versions, the expiration time may not be very accurate, with an error of 0-1 second.

Since Redis 2.6, the expiration time error has been reduced to 0-1 millisecond.

6. The influence of external factors on the expiration time

The expiration time of Keys is stored using Unix timestamps (in milliseconds starting with Redis 2.6). This means that time is running out even if the Redis instance is not available.

For overdue work to be handled well, the computer must use a stable time. If you synchronize RDB files between two computers whose clocks are out of sync, interesting things will happen (all keys will expire when they are loaded).

Even a running instance will check the computer's clock, for example, if you set a key valid for 1000 seconds, and then set your computer time to the next 2000 seconds, key will expire immediately instead of waiting for 1000 seconds.

How to eliminate expired keys by 7.Redis

There are two ways for Redis keys to expire: passive and active.

When some clients try to access it, key will be discovered and actively expired.

Of course, this is not enough, because there are some expired keys that will never be visited. In any case, these keys should expire, so scheduled random tests set the expiration time of the keys. All of these expired keys will be deleted.

This is exactly what Redis does 10 times per second:

1)。 20 random keys were tested for related expiration detection.

2)。 Delete all expired keys.

3)。 If more than 25% of the keys expires, repeat step 1.

This is a trivial probability algorithm, and the basic assumption is that our sample is the key control, and we keep repeating the expiration detection until the percentage of expired keys is less than 25%, which means that at any given time, the expired keys of 1 keys 4 will be cleared at most.

8. How to handle expiration when copying AOF files

In order to achieve the correct behavior without sacrificing consistency, when a key expires, the DEL will be synthesized into all additional slaves along with the AOF text. In the master instance, this approach is centralized and there is no chance of consistency errors.

However, when slaves connects to master, the keys does not expire independently (it waits for master to execute the DEL command), but they still exist in the dataset, so when slave is elected as master, the obsolete keys executes independently and then becomes master.

At this point, the study on "how to set the validity period of redis key" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report