redis caching strategy
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:
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?
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:
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?
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.
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.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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.
