PHP Caching Technology

Source: Internet
Author: User

Why use caching?

Caching technology is the most effective way to improve system performance. Caching technology is almost ubiquitous, from the temporary switching zone of the operating system, to the CPU's level one or two cache, to the hard disk graphics card. Specific to the Web application, from the browser address bar you type the address and hit enter to start, to the page presented in front of you, this time the network request, along the way, the use of the cache technology everywhere. DNS, routers, load balancer devices, proxy caches, Web servers, browsers, etc. are all in a variety of different forms in the use of caching technology.

The role of caching

Cache as a storage medium, save the results of the last time-consuming calculation, the next time the same request arrives, directly return to the saved results, no longer have to excuse the back-end server to do a duplication of work. In this way, the back-end server pressure is reduced, naturally can provide more services, this is a typical use of space for time-changing practices.

Features of the cache

The validity period is the necessary attribute of the cache, and when the validity period expires, the cache fails, recalculates the result and caches it, and enters the next aging period.

When the cache content changes, you need to actively delete the cache.

PHP Caching Technology

In the large direction, the PHP cache can be divided into compiled class cache, data class cache, reverse proxy-based Web cache and page static

    • Compiling the class cache

Common to the compiler cache tool has Apc,accelerator,xcache, and so on, the principle is to save the script compiled generated opcode, if the script file is not modified, the next time do not recompile (of course, the specific behavior is related to configuration).

For APC, in php.ini, setting Apc.ttl to 0 indicates that the cache entry never times out, which means that when the buffer is full, new entries cannot be cached, and the official recommended value is 7200~86400. The Apc.stat option indicates whether the script update check is enabled. The default value of 1 means that APC checks that the script is updated each time the script file is requested, and if the update is recompiled and cached, this has an impact on performance. If set to 0, this means that the script is not checked for updates, which can significantly improve performance, but if the script file is updated, you will need to restart the Web server or the fcgi process with a default value of 1.

This usage of APC is called ' System cache ', and there is a usage called ' User data cache ', which is used similar to memcached, and is not expanded here.

    • Data class Caching

This kind of cache, mainly refers to the cache of variables, database query results and so on. There can be many implementations, such as a resource ID key, or a query statement as a key to store, the carrier that hosts the cache data is usually Memcached,redis and files.

    • Reverse proxy-based Web caching

such as Nginx Proxy_cache and fastcgi_cache,apache2 of Mod_proxy and Mod_cache, as well as squid and varnish. Strictly speaking, this does not belong to the PHP cache, but given the tight combination with PHP, right vote is in one place.

With Nginx For example, there are two kinds of caching mechanisms, Proxy_cache and Fastcgi_cache, the difference between them:

The Proxy_cache function is to cache the contents of the backend server, which may be anything, including static and dynamic.
The Fastcgi_cache function is to cache fastcgi generated content, in many cases the dynamic content generated by PHP.

The Proxy_cache cache reduces the number of nginx-to-backend communications, saving transmission time and back-end bandwidth.
The Fastcgi_cache cache reduces the number of nginx-to-PHP communications and reduces the pressure on PHP and the database.

    • Page static

It can also be called page caching, which can be divided into full page cache and page static fragment cache. The main use of the technology for output control.

Ob_start (); The code to run *******$contentob_get_contents(); Write cached content to HTML file * * * * * * *file_put_contents(' abc.html ',$content); Ob_end_clean ();

This allows users to access abc.html directly. The page static fragment cache is similar, can be implemented with OB, or it can be implemented with ESI. In another case, most of the content of the page is static, but a small part of the content is dynamic, how to do it. In this case we belong to the dynamic cache technology, we'll talk about it next time.

PHP Caching Technology

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.