Various caching methods for PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Various caching methods of PHP are introduced. The WEB program obtains information mainly by querying the database. when the database is not very large, there will be no major problems. However, with the development of the website, the WEB program with a geometric level of database obtains information mainly by querying the database. if the database is not very large, there will not be too many problems. However, with the development of the website, the database grows exponentially, and the bottleneck occurs. Therefore, the PHP Cache technology was born. When the PHP Cache technology works, when the program queries data, It serializes the corresponding results and saves them to the file. in the future, the same query statement can be used without directly querying the database, instead, it is obtained from the cache file. This improvement improves the running speed.

Php Cache technology is widely used. some people may not know much about this technology. now we will give you a detailed introduction to the related application skills of PHP Cache technology.

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.

1. PHP Cache technology-Data cache

The data cache mentioned here refers to the PHP Cache mechanism for database query. when accessing the page every time, the system checks whether the cache data exists. if the cache data does not exist, it connects to the database to obtain the data, serialize the query results and save them to the file. later, the same query results will be obtained directly from the cache table or file. The most widely used example is the Discuz search function. The result ID is cached in a table and the table is searched in the cache next time when the same keywords are searched.

For example, when multiple tables are joined, an array generated in the appendix is saved to a field in the master table. if necessary, the array is decomposed, the advantage is read-only tables. The disadvantage is that two data synchronization steps are many more steps, and the database is always the bottleneck. Changing the disk speed is the key point.

2. PHP Cache technology-Page Caching

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)

3. PHP Cache technology-memory cache

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 solve the problem of frequent database queries, its disadvantage lies in the absence of timeliness of data.

4. PHP Cache technology-time-triggered caching

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.

5. PHP Cache technology-content-triggered caching

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.

6. 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.

In addition to the above caching methods, there are also some server-side caching solutions, not code-level:

  • Php buffer: there are eaccelerator, apc, phpa, and xcache. you don't need to talk about this. search for a bunch of them. you can check it yourself. if you know it is OK.
  • MySQL cache: this is not code-level. it is used in a classic Database. See the following running time, such as 0.09xxx.
  • Reverse proxy-based Web cache: such as Nginx, SQUID, mod_proxy (apache2 and above are also divided into mod_proxy and mod_cache)
  • DNS round robin: BIND is an open-source DNS server software. this is a big one. you can search for it by yourself. I know that chinacache and other major sites are doing this. Simply put, they are multiple servers and cache the same page or file on different servers, it is automatically resolved to the relevant server by north and south.

Bytes. However, with the development of the website, the database shows a geometric level...

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.