Home> Database> Redis> body text

How to implement caching in redis

(*-*)浩
Release: 2019-11-20 13:08:56
Original
7060 people have browsed it

How to implement caching in redis

Redis implements caching in roughly two ways:

1. Script synchronization:

1. Write your own script to write database data to redis/memcached. (Recommended learning:Redis video tutorial)

2. This involves the issue of real-time data changes (real-time analysis of mysql row binlog), binlog incremental subscription to Alibaba's canal, and caching Data synchronization recovery problem after layer data loss/invalidity.

2. Business layer implementation:

1. First read the nosql cache layer. If there is no data, then read the mysql layer and write the data to nosql.

2. The nosql layer is ready for multi-node distribution (consistent hashing), as well as alternatives after node failure (multi-layer hashing to find adjacent replacement nodes), and data shock has been restored.

Analysis of redis implementation of database caching:

For data that changes very quickly, if you still choose the traditional static caching method (Memocached, File System, etc.) Displaying data may involve a lot of overhead in cache access, which cannot meet the needs well. Memory-based NoSQL databases like Redis are very suitable for serving as containers for real-time data.

However, there are often requirements for data reliability. Using MySQL as data storage will not cause data loss due to memory problems. At the same time, you can also use the characteristics of relational databases to implement many functions. So it is natural to think about whether MySQL can be used as the data storage engine and Redis as the Cache.

MySQL to Redis data replication scheme, whether MySQL or Redis, has its own data synchronization mechanism. The more commonly used MySQL Master/Slave mode is implemented by the Slave side analyzing the Master's binlog, so Data replication is actually an asynchronous process, but when the servers are all on the same intranet, the asynchronous delay is almost negligible.

Theoretically, the same method can be used to analyze the MySQL binlog file and insert the data into Redis.

So here we chose a way with lower development costs, borrowing the already relatively mature MySQL UDF, first putting the MySQL data into Gearman, and then synchronizing the data to Gearman through a self-written PHP Gearman Worker. Redis. Compared with the method of analyzing binlog, it adds a lot of processes, but the implementation cost is lower and it is easier to operate.

The above is the detailed content of How to implement caching in redis. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!