In most cases, our website uses databases as the container for storing site data. When you execute an SQL query, the typical process is: connect to the database-> prepare an SQL query-> send a query to the database-> get the database return result-> close the database connection. However, some data in the database is completely static or infrequently changed. The cache system caches the SQL query results to a faster storage system for storage, this avoids frequent database operations and greatly increases the program execution time, and caches the query results for later processing.
Widely used PHP Cache Technology
Data caching in PHP Cache Technology:
The data cache mentioned here refers to the database query cache. Each time you access the page, it first checks whether the cache data exists. If it does not exist, it connects to the database to obtain the data, the query results are serialized and saved to the file. The same query results will be obtained directly from the cache file.
PHP Cache Technology-page cache:
Each time you access the page, the system first checks whether the corresponding cached page file exists. If it does not exist, it connects to the database to obtain data, displays the page, and generates cache page files at the same time, in this way, the page file will play a role in the next visit. (The template engine and some common cache classes on the Internet usually have this function)
Memory cache of PHP Cache Technology:
I will not introduce it here. It is not discussed in this article. I just want to mention it briefly:
Memcached is a high-performance, distributed memory object cache system that reduces database loads in dynamic applications and improves access speeds.
Dbcached is a Distributed key-value database memory cache system based on Memcached and NMDB.
Although the above caching technology can effectively solve the problem of frequent database queries, its disadvantage is that the data is not time-sensitive. Below I will provide a method that is commonly used in projects:
PHP Cache Technology-time-triggered cache:
Check whether the file exists and the timestamp is earlier than the set expiration time. If the modified timestamp of the file is greater than the current timestamp minus the expiration timestamp, use the cache; otherwise, update the cache.
During the set time, the system does not judge whether the data is to be updated. After the set time, the system updates the cache. The above is applicable only when the requirements for timeliness are not high. Otherwise, please refer to the following.
Content-triggered caching in PHP Cache Technology:
When data is inserted or updated, the cache is forcibly updated.
Here we can see that when a large amount of data frequently needs to be updated, disk read/write operations will be involved in the end. How can this problem be solved? In my daily projects, I usually do not cache all content, but cache some of the content that is not changed frequently. However, in the case of heavy load, it is best to use the shared memory as a cache system.
Here, PHP cache may be a solution, but its disadvantage is that, because every request still needs to be parsed by PHP, the efficiency problem is still serious under heavy load conditions, in this case, static cache may be used.
PHP Cache Technology-static Cache
The static cache here refers to the HTML cache. The HTML cache generally does not need to judge whether the data is to be updated, because the HTML cache usually does not change the content frequently. When the data is updated, you can force the HTML to be updated.