Summary of nine cache techniques in PHP

Source: Internet
Author: User
Tags apc php language php server php compiler php template smarty template

PHP cache includes both PHP compilation cache and PHP data cache. PHP is an interpreted language, which belongs to the side-compilation side of the run. The advantage of this mode of operation is that program modification is convenient, but the operation efficiency is very low. PHP compiler cache for this situation to improve processing, so that the PHP language only run once, you can put the program's compilation results cached.

______________________________________________________________________________________________________________

PHP compiled cache is currently the most common PHP compiler caching tools are: Apc,accelerator,xcache (domestic) and so on. PHP is ainterpreted language, in the PHP language code execution, need the following two steps: 1, the compilation process. PHP reads the file, compiles the file, and then generates an intermediate code that can be executed on the Zend engine virtual machine. 2, the implementation process. PHP executes the intermediate code directly. The following two scenarios cause PHP to run program code inefficiently:1, even if the PHP code file does not change, it will be recompiled by PHP. 2, if there is a reference file, PHP will also take the time to recompile these referenced files. Therefore, the PHP compilation cache tool is required to cache the compiled results of the PHP program. This way, the PHP program only compiles once, do not re-make meaningless compilation. PHP Data cache PHP Data caching includes caching for database data and caching for PHP template data. The tools for caching database data are memcache and so on. The main tools for caching PHP template data are smarty and so on.

_______________________________________________________________________________________________________________ __

PHP Cache Type 1, database Data caching technology:Data cache: In this case, the data cache refers to the database query PHP cache mechanism, each time you visit the page, will first detect the corresponding cache data exists, if not exist, connect to the database, get the data, and the query results are serialized and saved to the file, Later, the same query results are obtained directly from the cache table or file. The most widely used example is the search function of discuz, which caches the result ID into a table and searches the cache table the next time the same keyword is searched. and Memcache technology. For a common method, multi-table association, the schedule of the contents of the array is saved to a field in the main table, the need for the array decomposition, the advantage is only read a table, the disadvantage is that two data synchronization will be more than a few steps, the database is always the bottleneck, with the hard disk speed, is the key point. Common database data caching techniques are: 1. Serialization (serialized) cache 2.JSON Cache 3.XML Cache 4.Array Cache 2. Page cache:Each time you visit the page, will detect the corresponding cache page file exists, if not exist, connect to the database, get data, display the page and generate the cache page file at the same time, so the next visit to the page file will play a role. (the template engine and some common PHP caching mechanism classes on the web typically have this functionality, such as the Smarty template, the thinkphp framework) ____________________________________________________________ _________________________________________________________1, full page static cache

That is, the page is all generated HTML static page, the user accesses the static page directly, but not to go to the PHP server parsing process. This way, in the CMS system is more common, such as dedecms;

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

Ob_start()******要运行的代码*******$content = Ob_get_contents();****将缓存内容写入html文件*****Ob_end_clean();
2. Page Partial cache

In this way, the infrequently changed parts of a page are statically cached, and the frequently changing blocks are not cached, assembled and displayed together, can be implemented in a way similar to ob_get_contents, or can take advantage of page fragment caching policies like ESI, The cache used to make a relatively static fragment portion of a dynamic page (ESI technology, please Baidu, here is an unknown).

This method can be used for product pages such as the mall;

3. Data cache

As the name implies, is a way to cache data, for example, a product information in the mall, when the product ID to request, will be included in the store information, merchandise information and other data, this can be cached in a PHP file, the file name contains the product ID to build a unique mark The next time someone wants to view this product, the first thing is to directly tune the information inside the file, instead of going to the database query; in fact, the cache file is a PHP array.

Ecmall Mall system inside the use of this way;

4. Query cache

In fact, this with the data cache is a way of thinking, is based on query statements to cache, the query to get the data cached in a file, the next time you encounter the same query, directly from the file to adjust the data, will not check the database, but the cache file name here may need to use a query statement as the base point to establish a unique;

Cache by Time Change

In fact, this is not a true caching method, the above 2, 3, 4 of the cache technology is generally used in the time to change the judgment, that is, for the cache file you need to set a valid time, in this effective time, the same access will first take the contents of the cache file, but more than the set cache time, You need to re-fetch the data from the database and produce the latest cache files; For example, I will be the first page of our mall is set up 2 hours to update;

5. Cache by content Change

This is not an independent caching technology, need to be combined with, that is, when the database content is modified, immediately update the cache file;
For example, a large number of shopping malls, a lot of goods, commodity tables must be relatively large, the pressure of the table is also heavier; we can cache the product display page;

When the merchant changes the information of this product in the background, click Save, we update the cache file at the same time, then, when the buyer accesses this product information, it actually accesses a static page, and does not need to visit the database again;

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

6. In-Memory cache

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

It is to cache the information that needs to be cached in the system memory, need to obtain information, directly into the memory, the more common way is key–>value way;

<?PHP $memcachehost= ' 192.168.6.191 ';$memcacheport= 11211;$memcachelife= 60;$memcache= New Memcache;$memcache-connect ( $memcachehost  $memcacheport )  or Span class= "KWD" >die  ( "Could not connect"  $memcache ->set (, ' cached content '  '  $get =< Span class= "PLN" > $memcache ->get ( $key  //get information ?>     
7. Apache Cache Module

After the installation of Apache, the cache is not allowed. If you have a cache or squid server that requires web acceleration, you need to set it up in htttpd.conf, as long as you activate the Mod_cache module when you install Apache.

When installing Apache:./configure–enable-cache–enable-disk-cache–enable-mem-cache

8. PHP APC Cache extension

PHP has an APC cache extension, Windows is php_apc.dll, you need to load the module first, and then configure it in php.ini:

[Apc]Extension=Php_apc.DLL APC.rfc1867=On Upload_max_filesize= 100MPost_max_size= 100MApc.Max_file_size= 200MUpload_max_filesize= 1000MPost_max_size= 1000M Max_execution_time = 600  ; each php page run maximum time value (seconds), default 30  seconds  Max_input_time = 600< Span class= "PLN" > ; each php page The maximum time required to receive data, default 60  Memory_limit = 128m Span class= "pun" >;   each php The maximum memory eaten by the page, default 8m   
9. OpCode Cache

We know that PHP's execution process can be used to show:

First, the PHP code is parsed into tokens, and then compiled into opcode code, and finally execute opcode code, return the results; So, for the same PHP file, the first time the runtime can cache its opcode code, the next time you execute this page, will go to find the cache opcode code , take the final step directly, without needing the intermediate steps.

More well-known is XCache, Turck MM Cache, PHP Accelerator and so on.

Summary of nine cache techniques in PHP

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.