Redis cache policy

  mysql, question

In the process of using redis to cache data, the idea of designing cache is as follows:
When obtaining data, first obtain the data from the cache, if the obtained data is empty, query the database, then cache the queried data to redis, and then return the data
But now there are two questions to ask you:

  1. If the result of the query in the database is empty, the data will not be cached, so the data cannot be obtained from the cache. Then a database query will be made every time the data is obtained. How should this be optimized?

  2. What should cache do when data is updated?

I have a little immature idea on these two issues. Now I would like to ask you to give me some advice on whether it is feasible and whether there is a better way to deal with them:

For the first question, if nodata is obtained from the database query, then caching a fixed default value (such as string:’ no data’) means no data, and the next time data is obtained from the cache, obtaining the fixed value indicating no data will directly return null without querying the database again

For the second question, the current idea is that if the updated data is not particularly important, the corresponding data in the cache will be updated directly after the data is written into the database, and the latest data can be obtained directly without querying the database the next time the data is obtained. However, if the updated data is particularly important (e.g. money), the corresponding cache will be cleared directly after the data is written into the database, and the database will need to be queried to obtain the latest data the next time the data is obtained.

  1. You need a way (such as natural nullable type or and type) to distinguish the two situations. For example, when there is data, there is a JSON list stored inside[{...}, {...}], then no data naturally exist[]Here we go. In short, there is no conflict.

  2. Cache Update Routine | Cool