PHP Caching Technology Summary

Source: Internet
Author: User
Tags apc memcached php file php server php code valid

full-page static caching

That is, the page is all generated HTML static page, user access to direct access to the static page, and will not go to the PHP server parsing process. This kind of way, in the CMS system is more common, for example dedecms;

A more common way to implement this is to use output caching:

Ob_start ()

The code to run *******

$content = Ob_get_contents ();

Write cached content to an HTML file * * *

Ob_end_clean ();

  Page Partial Caching

In this way, the infrequently variable parts of a page are statically cached, and the frequently changing blocks are not cached and finally assembled together to display; You can do this in a ob_get_contents way, or you can use a page fragment caching strategy like ESI, The cache that is used to make the relatively static fragment portion of the dynamic page (ESI technology, please Baidu, here is unknown).

This kind of way can be used for commodity page such as mall;

  Data caching

As the name suggests, is a way of caching data; For example, a store in a commodity information, when using the Product ID to request, will be drawn to include shop information, commodity information, and so on, this data can be cached in a PHP file, the filename contains the product ID to build a unique indicator; The next time someone wants to look at this product, the first directly to the file inside the information, and no longer go to the database query; in fact, cached in the cache file is a PHP array, and so on;

Ecmall Mall system inside the use of this way;

  Query caching

In fact, this is a way of thinking with data caching, is based on the query statement to cache, the query to get the data cached in a file, the next encounter the same query, directly from the file to adjust the data, no longer to check the database; however, the cached filename here may need to be based on the query statement to establish a unique indicator;

  Cache by Time Change

In fact, this one is not the real caching method; The above 2, 3, 4 of the caching techniques are generally used to determine the time change; for the cached file you need to set a valid time, within this valid time, the same access will first take the contents of the cached file, but more than the set cache time, You need to retrieve the data from the database and produce the latest cached files;

For example, I will be the first page of our mall is set up 2-hour update;

  Cache by content Change

This is not an independent caching technology, it needs to be combined with; when the contents of the database are modified, the cached files are updated immediately;

For example, a large pedestrian flow of the mall, a lot of goods, the commodity table must be larger, the pressure of the table is also heavier; we can cache the product display page; When the merchant changes the information in the background, click Save, we update the cached file at the same time; then, when the buyer visits this product information, Actually accessing a static page without having to access the database;

is to think, if the product page does not cache, then each visit to a commodity database to check once, if there are 100,000 people online browsing merchandise, the server pressure is big;

  Memory-Cache

The first thing you can think of about this is that memcached;memcached is a high-performance distributed memory cache server. The purpose of the general use is to reduce the number of database visits by caching database query results to improve the speed and scalability of dynamic Web applications.

It will need to cache the information, cached in the system memory, need to obtain information, directly to the memory to take; the more common way is key-->value way;

$memcachehost = ' 192.168.6.191 ';

$memcacheport = 11211;

$memcachelife = 60;

$memcache = new Memcache;

$memcache->connect ($memcachehost, $memcacheport) or die ("could not Connect");

$memcache->set (' key ', ' cached content ');

$get = $memcache->get ($key); Get information

?>

  Apache Cache Module

After the Apache installation, is not allowed to be cache. If the external cache or squid server requirements for Web acceleration, you need to set up in the htttpd.conf, of course, if you are installing Apache to activate the Mod_cache module.

When you install Apache:./configure--enable-cache--enable-disk-cache--enable-mem-cache

  PHP APC Cache Extension

PHP has an APC cache extension, the following windows for Php_apc.dll, the need to load the module, and then in the php.ini to configure:

[APC]

Extension=php_apc.dll

apc.rfc1867 = On

Upload_max_filesize = 100M

Post_max_size = 100M

Apc.max_file_size = 200M

Upload_max_filesize = 1000M

Post_max_size = 1000M

Max_execution_time = 600; Maximum time for each PHP page to run (seconds), default 30 seconds

Max_input_time = 600; The maximum time required for each PHP page to receive data, default 60

Memory_limit = 128M; The maximum memory consumed by each PHP page, default 8M

  OpCode cache

We know that the PHP execution process can be shown in the following diagram:

First PHP code is resolved to tokens, and then compiled to opcode code, the final execution of the opcode code, return the results; So, for the same PHP file, the first run can cache its opcode code, the next time you execute this page, will go directly to the cache to find the opcode code , take the final step directly and no longer need the intermediate steps.

More well-known is XCache, Turck MM Cache, PHP accelerator, etc.;

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.