Caching, many sites are used, especially when dealing with high concurrency, where caching is essential. This article is based on PHALAPI to explain the use of the cache Redis in practice, we hope to help.
Objective
When we develop a project, we may encounter a lot of problems, such as message push, send email, send text messages, and concurrency, this time it is the usual cache shot to rescue us, we will talk about the use of cache Redis in practice, Solve practical problems. Here is the basics of Redis based, and a simple look at Phalapi's Redis extension document is coming to read this section.
1. What problems can be solved
When we use a technology, of course, we use it to solve the problem, then we use the cache technology Redis can solve what specific problem?
1.1 Cache result Set
Here's an example of what the cache result set means when you look at it.
Query the club table from the cache Redis Clubcache Library where condition is the city,city value is $city$cache = DI ()->redis->get_time (' Club '. City '. $city, ' Clubcache ');//If the query is made, return the cached result directly if ($cache) { return $cache;} The last parameter is the expiration time $rs = $this->getorm ()->select (' * ')->where (' City ', $) if there is no condition and value for obtaining results from the database and then depositing it into the Redis cache key. City)->fetchall ();D I ()->redis->set_time (' Club '. City '. $city, $rs, ' Clubcache ', 600);
The thing to do is save the result for 600 seconds, and the query in 600 seconds will get the same result.
1.2 Queue Processing
One of the more critical roles Redis uses in time is his queue
Let's go through a few special Redis functions.
Write queue left set_lpush//write queue left if value already exists, do not add set_lpushx//write queue to the right set_rpush//write queue to the right if value already exists, do not add set_rpushx// Read queue left get_lpop//read queue right get_rpop//read queue left if not read to block for a certain time get_blpop//read queue right if not read to block for a certain time Get_brpop
For example, we do the message push, send mail, send SMS such business, we need to request a third-party interface, the speed of the request is a third-party decision, such as a push interface is 200ms, if put into our API business inside there will be a huge problem, the user access speed is extremely degraded, The solution to this kind of problem is the queue process as follows
When we receive a push request from a user
↓
Add push requests to the queue API without doing anything (such as adding to the left)
↓
In the background there is a PHP script that runs all the time in the read queue (reading to the right is backward, if Read Left is FIFO)
↓
Then the push logic that executes the response
Generally our script is a dead loop, or a shell timed request, we will take the data is not read is blocking to solve the problem of not going to the value loop too fast
1.3 Temporary data storage
The temporary data doesn't need a lot of explaining, and for example, it's enough.
For example, we get the verification code, we need to put the verification code in the library, I think it is not necessary, and the database is not good to do the expiration of the operation can only be judged by ourselves
Then we use Redis to put the verification code into Redis and give an expiration time to solve the problem well
1.4 Database
Using Redis as a database is a more in-depth use, here to talk about ideas
Everyone after the service can be distributed, but for most of the database distribution is not easy, so caused a lot of system to the back stitching piled up in the database, of course, you can use the cache to store the result set, but this solution is easy to solve the problem, in and children's shoes when the discussion reached a convenient solution, is to use Redis as the first database for MySQL as a meta database
After doing this, the server automatically synchronizes the hot data to Redis, storing the cold data to MySQL, and when the cold data is stored on the Redis, the majority of the user's operations are based on Redis.
As the cost of achieving the implementation of the Redis data synchronization package using where query and so on need a lot of effort to do, in the late I have plans to do a general expansion
2. Normalized use
In fact, the above class capacity has been said to be almost, why there is a separate section to say a specification it, because the cache is not like a database when you need to check the cache, if all the data are stacked in a Redis library, you will be very painful
But Redis supports multiple libraries so you need a set of specs to share, and here's how I'm going to use this.
0~10 Library as a normal business library, that is, the push queue, temporary data, each library only store a business data, such as the existence of a push 5 libraries, and mail push the data there are 6 libraries, send verification code temporary data stored in 3 libraries, and so on, if you think 10 libraries are not enough to increase according to the business
More than 10 libraries are used as cache libraries to store the result set data for each table, or the rest of the data
All key naming conventions must have a type + table name + condition
Related recommendations:
Example Analysis of PHP file cache class Usage
PHP caching mechanism
Analysis of Redis cluster failure