This article gives a brief overview of the expiration processing mechanism of redis to give everyone a basic understanding.
#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!

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
