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

Expiration handling Mechanism of redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This paper gives a brief overview of the expiration mechanism of redis, so that we can have a basic understanding.

There is a function of setting time expiration in Redis, that is, you can set an expiration time for values stored in the redis database. As a cached database, this is very practical.

For example, the token or some login information in our general project, especially the SMS verification code, is time-limited. According to the traditional database processing method, we generally judge the expiration date by ourselves, which will undoubtedly seriously affect the project performance.

1. Valid time setting:

The expiration of the stored value by redis is actually handled for the key (key) of that value, that is, the setting of time is also the valid time for setting key. The Expires dictionary holds the expiration time of all keys, and Expires is also known as the expiration field.

Four processing strategies

EXPIRE sets the lifetime of key to ttl seconds

PEXPIRE sets the generation time of key to ttl milliseconds

EXPIREAT sets the expiration time of key to the timestamp of the number of seconds represented by timestamp

PEXPIREAT sets the expiration time of key to the timestamp of milliseconds represented by timestamp

In fact, the above processing methods are all based on PEXPIREAT. When setting the survival time, it is processed in memory after the redis calculates the time internally, and the final processing will turn to PEXPIREAT.

1, 2 two ways is to set an expiration period, that is, we deal with the most commonly used strategy of CAPTCHA, set it to expire after three or five minutes, and convert the minutes into seconds or milliseconds and store them in redis.

3 and 4 the two ways are to specify an expiration time, for example, the expiration time of the coupon is a certain year, month and day, but the unit is different.

II. Expiration treatment

The processing of expired keys is to delete expired keys, and the operation here is mainly for expired fields.

There are three processing strategies in Redis: scheduled deletion, lazy deletion, and periodic deletion.

Scheduled deletion: create a timer when setting the expiration time of the key and perform the delete operation immediately when the expiration time is up. However, this method of processing is immediate, no matter how many expired keys there are during this time, regardless of the current health status of the server, it will be executed immediately, so it is not very friendly to CPU.

Lazy deletion: the lazy delete policy is not deleted immediately when the key expires, but is actively deleted when the external instruction acquires the key. The processing process is as follows: receiving get execution, judging whether it expires (here judging by expiration), performing deletion operation, and returning nil (null).

Periodic deletion: periodic deletion is to set a time interval, each time period will detect whether there is an expired key, if a delete operation is performed. This concept should be easy to understand.

The above are the details of where the redis validity period is set, please pay more attention to other related 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

Database

Wechat

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

12
Report