Home  >  Article  >  Backend Development  >  redis caching strategy

redis caching strategy

WBOY
WBOYOriginal
2016-09-06 08:57:081192browse

In the process of using redis to cache data, the cache design idea is as follows:
When obtaining data, first obtain the data from the cache. If the obtained data is empty, query the database, and then cache the queried data to redis first. , and then return the data
But now I have two questions for you to ask:

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

  2. When data is updated, how should the cache be handled?

I have some immature ideas about these two issues. I would like to ask everyone to give me some advice on whether this is feasible and whether there is a better way to deal with it:

For the first question, if no data is obtained from the database query, then cache a fixed default value (such as string: 'nodata') to indicate that there is no data. The next time you obtain data from the cache, you will get this fixed value to indicate that there is no data. When the value of the data is returned, null is returned directly without querying the database again

For the second question, the current idea is that if the updated data is not particularly important, then write the data to the database and directly update the corresponding data in the cache. The next time you get the data, you can directly get the latest data without querying the database. However, if the updated data is particularly important (for example: money), then the corresponding cache will be cleared directly after the data is written to the database. The next time you obtain the data, you will need to query the database to obtain the latest data.

Reply content:

In the process of using redis to cache data, the cache design idea is as follows:
When obtaining data, first obtain the data from the cache. If the obtained data is empty, query the database, and then cache the queried data to redis first. , and then return the data
But now I have two questions for you to ask:

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

  2. When data is updated, how should the cache be handled?

I have some immature ideas about these two issues. I would like to ask everyone to give me some advice on whether this is feasible and whether there is a better way to deal with it:

For the first question, if no data is obtained from the database query, then cache a fixed default value (such as string: 'nodata') to indicate that there is no data. The next time you obtain data from the cache, you will get this fixed value to indicate that there is no data. When the value of the data is returned, null is returned directly without querying the database again

For the second question, the current idea is that if the updated data is not particularly important, then write the data to the database and directly update the corresponding data in the cache. The next time you get the data, you can directly get the latest data without querying the database. However, if the updated data is particularly important (for example: money), then the corresponding cache will be cleared directly after the data is written to the database. The next time you obtain the data, you will need to query the database to obtain the latest data.

  1. You need a way (such as a naturally nullable type or a sum type) to distinguish between these two situations. For example, when there is data, it stores a JSON list [{...}, {...}], then when there is no data, it will naturally store []. As long as there is no conflict.

  2. Cache update routine | Cool Shell - CoolShell.cn

What you said above makes sense.
You can refer to the idea of ​​file caching.
Thinkphp file caching uses serialize (valle) to save data, and the file name is used as the key name. To clear the cache, directly set(null) to achieve it.
Caching is only for convenience when querying and reducing the number of mysql accesses. As for the cache update problem, it must be implemented by code.

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
Previous article:thinkphp problemNext article:thinkphp problem