Redis 缓存和数据库之间的数据校验该怎么做?
怪我咯
怪我咯 2017-04-22 08:56:29
0
3
754

数据库是 MySQL,Redis 主要用来读缓存,现需要检测数据和缓存之间的一致性,确保从缓存读取的数据是最新的。该怎么确保这一点呢?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
刘奇

1. When instantiating a model, such as when querying, you can save the total number of records in the table, the last modification time and other information to a method of the model
2. When querying data, combine the query conditions and table information to calculate a cache key and write it into the cache
3. When obtaining the cache, combine the query conditions and table information to calculate a cache key and query the cache. If the table data is modified and the cache cannot be hit, the latest result will be obtained and saved as a new key

In actual development, the MVC framework is used. During the CUD operation of the model, all cache keys associated with the model will be automatically deleted to ensure data consistency.
This requires relatively high standards for the use of the framework, and the cache needs to be registered according to the model caching specifications.

A typical application at present is how to update the cached data of the list after CRUD.

刘奇

Check the cache, if not, dump the data in the database to the cache.
If there is a data update, if the data consistency is not very high, then just give the data in the cache a ttl; if the data consistency is very high, clear the cache when updating the data.

伊谢尔伦

You can add a field to each key to represent version information. Update version number of updated data, use key+version number when querying redis.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template