Redis Cache Policy

Source: Internet
Author: User
In the process of using Redis to cache data, the idea of designing a cache is as follows:
When the data is fetched, the data is fetched from the cache, and if the obtained data is empty, the database is queried and the queried data is cached to Redis before the data is returned
But now there are two questions to ask you:

    1. If the result of the query in the database is empty, then the data is not cached and therefore cannot be fetched from the cache, then a database query is made every time the data is fetched, how should this be optimized?

    2. What should the cache do with the data when it is updated?

I have a little immature idea of these two issues, and now I would ask you to point out whether it is feasible and whether there is a better way to deal with it:

For the first question, if the data is not obtained from the database query, then caching a fixed default value (such as the string: ' NoData ') indicates no data, and the next time the data is fetched from the cache, the fixed representation of the value without data is returned empty without querying the database again

For the second question, the current idea is that if the updated data is not particularly important, then after the data is written to the database directly update the corresponding data in the cache, the next time you get the data without querying the database can directly get the latest data, but if the updated data is particularly important (such as: money), After the data is written to the database, the corresponding cache is purged, and the next time you retrieve the data, you need to query the database for the latest data.

Reply content:

In the process of using Redis to cache data, the idea of designing a cache is as follows:
When the data is fetched, the data is fetched from the cache, and if the obtained data is empty, the database is queried and the queried data is cached to Redis before the data is returned
But now there are two questions to ask you:

    1. If the result of the query in the database is empty, then the data is not cached and therefore cannot be fetched from the cache, then a database query is made every time the data is fetched, how should this be optimized?

    2. What should the cache do with the data when it is updated?

I have a little immature idea of these two issues, and now I would ask you to point out whether it is feasible and whether there is a better way to deal with it:

For the first question, if the data is not obtained from the database query, then caching a fixed default value (such as the string: ' NoData ') indicates no data, and the next time the data is fetched from the cache, the fixed representation of the value without data is returned empty without querying the database again

For the second question, the current idea is that if the updated data is not particularly important, then after the data is written to the database directly update the corresponding data in the cache, the next time you get the data without querying the database can directly get the latest data, but if the updated data is particularly important (such as: money), After the data is written to the database, the corresponding cache is purged, and the next time you retrieve the data, you need to query the database for the latest data.

    1. You need a way to differentiate between these two situations, such as natural nullable types or types. For example, when there is data, there is a JSON list [{...}, {...}] , then no data is stored naturally [] . In short, there is no conflict on the line.

    2. The routines for caching updates | Cool Shell-coolshell.cn

It makes sense to say it upstairs.
You can refer to the idea of file caching,
thinkphp file cache takes serialize (Valle) to save the data, the file name as the key name. Clear cache, direct set (null) to implement.
Cache is just for the convenience of query time, reduce the number of MySQL access, as for the cache update problem depends on the code to implement.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.