Home > Database > Redis > body text

Where is the validity period of redis set?

(*-*)浩
Release: 2019-11-30 10:18:46
Original
7268 people have browsed it

This article gives a brief overview of the expiration processing mechanism of redis to give everyone a basic understanding.

Where is the validity period of redis set?

#There is a function to set time expiration in Redis, that is, you can set an expiration time for the value stored in the redis database. As a caching database, this is very practical.

For example, tokens or some login information in our general projects, especially SMS verification codes, are time-limited. According to the traditional database processing method, you generally judge the expiration by yourself, which will undoubtedly seriously affect the project. performance. (Recommended learning: Redis video tutorial)

1. Valid time setting:

Redis’s expiration processing of stored values ​​​​actually It is processed for the key of the value, that is, the setting of the time is also the effective time of setting the key. The Expires dictionary stores the expiration time of all keys. Expires is also called the expiration field.

Four processing strategies

EXPIRE Set the key's survival time to ttl seconds

PEXPIRE Set the key's generation time to ttl Milliseconds

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

PEXPIREAT Set the expiration time of the key to the timestamp of the number of milliseconds represented by timestamp

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

The two methods 1 and 2 are to set an expiration time period, which is the most commonly used strategy for processing verification codes. Set it to expire after three minutes or five minutes, and convert the minutes into seconds or milliseconds and store them in redis middle.

The two methods 3 and 4 are to specify an expiration time. For example, the expiration time of a coupon is a certain day of a certain year, a certain month, but the units are different.

2. Expiration processing

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

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

Timed deletion: Create a timer when setting the expiration time of the key, and perform the deletion operation immediately when the expiration time reaches. However, this processing method is instant. No matter how many expired keys there are within this time, regardless of the current operating status of the server, it will be executed immediately, so it is not very friendly to the CPU.

Lazy deletion: The lazy deletion strategy will not delete the key immediately when it expires, but will actively delete it when an external instruction obtains the key. The processing process is: receive get execution, determine whether it is expired (here judged by expiration), perform the delete operation, and return nil (empty).

Periodic deletion: Periodic deletion is to set a time interval. Each time period will detect whether there is an expired key, and if so, perform a deletion operation. This concept should be easy to understand.

For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of Where is the validity period of redis set?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template