Home  >  Article  >  Database  >  How to use redis to cache mysql

How to use redis to cache mysql

(*-*)浩
(*-*)浩Original
2019-11-21 11:47:204109browse

In actual projects, the MySQL database server is sometimes located on another host and needs to access the database through the network; even if the application and the MySQL database are on the same host, accessing MySQL also involves disk IO operations (MySQL also has some Data pre-reading technology can reduce disk IO reading and writing, this part will be studied later).

How to use redis to cache mysql

#In short, reading data directly from MySQL is not as efficient as reading data directly from memory. In order to improve the efficiency of database access, people have adopted various methods, one of which is to use a memory-based cache system placed between the database and the application.                                                                                                                                                                                                  (Recommended learning: Redis Video Tutorial)

When looking for data, first search it from the memory, if it is found, use it, if not found, then actually access the database. This method can improve the overall efficiency of the system in some scenarios (for example, frequently searching for the same data).

Use the redis nosql database as the cache of the Mysql database. When searching, first search the redis cache, and if found, return the result; if not found in redis, then search the Mysql database, If the flower is found, the result is returned and redis is updated; if not found, empty is returned.

In the case of writing, write directly to the mysql database, and the mysql database automatically updates the changed content to redis through triggers and UDF mechanisms.

Block diagram:

How to use redis to cache mysql

##Reading steps:

1. The client reads redis, and if there is a hit, the result is returned. If there is no hit, go to 2.

2. The client reads the database, and if it is not found in the database, it returns empty; if it is found in the database, it returns the found result. Result and update Redis.

Writing steps:

1. The client modifies/delete or adds data to MySQL.

2. MySQL trigger calls user-defined UDF.

3. UDF updates modified/deleted or newly added data to redis.

The above is the detailed content of How to use redis to cache mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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