PHP 9 Large Cache Technology Summary _php example

Source: Internet
Author: User
Tags apc memcached php server

1, full page static cache

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 ();

2, page part cache

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;

3. Data caching

As the name suggests, is a way of caching data; For example, a commodity in the mall information, when using the Product ID to request, will be drawn to include shop information, merchandise information, etc., this data can be cached in a PHP file, the filename contains the product ID to build a unique logo The next time someone wants to view this product, first directly to the file inside the information, and do not 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;

4. Query cache

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;

5. 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 number of people in the mall, a lot of merchandise, the commodity table must be relatively large, the pressure of the table is also heavier; we can cache the display page of the product;

When the merchant in the background to modify the information of this product, click Save, we also update the cache file; Then, the buyer access to this product information, actually visit a static page, and do not need to access the database;

Imagine, 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;

6, Memory-type 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;

<?php 
   $memcachehost = ' 192.168.6.191 ';
   $memcacheport = 11211;
   $memcachelife =;
   $memcache = new Memcache;
   $memcache->connect ($memcachehost, $memcacheport) or die ("could not Connect");
   $memcache->set (' key ', ' cached content ');
   $get = $memcache->get ($key);    Get information
?>

7, 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

8, PHP APC Cache expansion

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:

   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 =;  Maximum time for each PHP page to run (seconds), default 30 seconds 
   max_input_time =;    The maximum time required for each PHP page to receive data, default 
   memory_limit = 128M;    The maximum memory consumed by each PHP page, default 8M

9, 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.

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.