According to the granularity from coarse to fine:
Take the product details page as an example
1. Page level caching
(Sometimes also called url level cache, the page is manually rendered and saved to redis (key=product id, val="product details page. When this page is requested again in the future, it is obtained from redis and returns text/html directly to the front end, and You can tell the browser to cache the page locally. The disadvantage is that the page data is not updated in time)
2. Object-level caching
(such as product data, (key= product id, val="database "Find product data") will save the product information that has been queried once to redis. When requesting to change the product data for the second time, redis will be used first to query)
The idea of redis caching is:
1. First check whether there is data in redis
1.1. If there is, return
1.2. If not, query the database, store it in redis, and then return
2. Pay attention to caching Life cycle,
If the cycle is permanent, then redis will easily collapse, it is just a matter of time
Use of redis in flash sale:
1. First kill the flash Product inventory is pre-stored in redis
2. After the flash sale starts, the inventory is pre-decreased in redis and reduced to 0. The product flash sale ends (redis is single-threaded)
3. If in order to reduce the For the access pressure of redis, the flash sale request submitted by the user can be placed in mq (such as RabbitMq).
For example, there are only 10 products in total, and a total of 100,000 users are eyeing them, all submitting flash sale requests at almost the same time,
3.1. You can put the request in the message queue, and return it to the front end as "queuing";
3.2. The consumer (the "consumer" of the queue) reads from the message queue at a fixed speed Get the data and create orders to the database (that is, create 10 orders in an orderly manner, with 0 impact on the database),
3.3. Although there may be users ranked in the top 10, if the creation of the order fails for some reason, The user's flash sale request will be placed at the end of mq, and then orders will be created for other users in the queue
3.4. Create a successful order request, remove the request from mq, and send a text message to the user." Congratulations, the flash sale is successful! "
3.5. After successfully creating 10 orders (that is, the flash sale ends), request other users in the queue to return "Second sale failed"
For more redis knowledge, please pay attention redis introductory tutorial column.
The above is the detailed content of Introduction to redis multi-level cache. For more information, please follow other related articles on the PHP Chinese website!