Php Tutorial: summary of the nine major cache technologies in PHP

Source: Internet
Author: User
[Guide] 1. static cache of the entire page, that is, generating all pages into html static pages. the static pages accessed by users are not parsed by the php server. This method is common in CMS systems, such as dedecms. a common implementation method is to use the output cache: ob [Guide] 1. static cache of all pages, that is, static html pages are generated on all pages. static pages accessed by users are not parsed by the php server. This method is common in CMS systems, such as dedecms. a common implementation method is output cache: Ob.

1. static full-page cache

That is, generate all html static pages. the static pages accessed by users are not parsed by the php server. This method is common in CMS systems, such as dedecms. a common implementation method is to use the output cache: Ob_start () * ****** the code to be run ******* $ content = Ob_get_con

2. this method is used to cache part of the page,

A static cache is used to cache infrequently changed parts of a page, but frequently changed blocks are not cached and finally assembled for display. it can be implemented in a way similar to ob_get_contents, you can also use a page fragment caching policy like ESI to cache relatively static parts of a dynamic page (ESI technology, please refer to baidu for details here ).
This method can be used, for example, on the product page in the mall;
3. data cache, as its name implies,

It is a way to cache data. for example, when a product information in the mall is requested by the product id, data including store information and product information will be obtained, in this case, you can cache the data to a php file. the file name contains the item id to create a unique identifier. The next time someone wants to view this item, first, directly call the information in this file, instead of querying the database; in fact, the cache file caches a php array and the like; this method is used in the Ecmall mall system;
4. query cache

In fact, this is a way of thinking with data cache, that is, to cache data based on query statements; to cache the queried data in a file, the next time you encounter the same query, you can call the data directly from the file without querying the database. However, the cache file name here may need to create a unique identifier based on the query statement;
Caching by time changes is actually not the real cache method. the above cache Technologies 2, 3, and 4 generally use the time change judgment; that is, you need to set a validity period for the cached file. during this validity period, the same access takes the content of the cached file first, but exceeds the specified cache time, you need to retrieve data from the database again and produce the latest cached files. for example, I set the homepage of our store to be updated once every two hours;
5. cache based on content changes

This is not an independent caching technology, and should be used in combination; that is, when the database content is modified, the cache file is immediately updated;
For example, in a mall with a large volume of traffic and a large number of commodities, the commodity table must be relatively large, and the pressure on this table is also heavy; we can cache the page of the commodity display page;
When a merchant modifies the product information in the background, click save and we will update the cache file at the same time. when the buyer accesses the product information, it actually accesses a static page, instead of accessing the database;
Imagine that if the product page is not cached, the database will be checked every time a product is accessed. if 0.1 million people browse the product online, the server will be under heavy pressure;
6. memory cache

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, so as to speed up dynamic Web applications and improve scalability.
It caches the information to be cached to the system memory. when you need to obtain the information, you can directly retrieve it from the memory. the more common method is the key-> value method;

$ Memcachehost = '2017. 168.6.191 '; $ memcacheport = 11211; $ memcachelife = 60; $ memcache = new Memcache; $ memcache-> connect ($ memcachehost, $ memcacheport) or die ("cocould not connect"); $ memcache-> set ('key', 'cached content'); $ get = $ memcache-> get ($ key ); // Obtain information?>

7. the apache cache module cannot be cached after apache is installed. If the external cache or squid server requires web acceleration, you need to set it in htttpd. conf, provided that the mod_cache module is activated during apache installation. When installing apache:./configure-enable-cache-enable-disk-cache-enable-mem-cache
8. php APC cache extension Php has an APC cache extension. in windows, php_apc.dll is used. you need to load this module and configure it in php. ini: [apc]

Extension = php_apc.dll apc. rfc1867 = on upload_max_filesize = 100 M post_max_size = 100 M apc. max_file_size = 200 M upload_max_filesize = 1000 M post_max_size = 1000 M max_execution_time = 600; maximum time (in seconds) for running each PHP page. the default value is 30 seconds. max_input_time = 600; maximum time required for receiving data on each PHP page. the default value is 60 memory_limit = 128 M. The maximum memory consumed by each PHP page is 8 M by default.

9. Opcode cache we know that the php execution process can be used to demonstrate:

First, the php code is parsed to Tokens, and then compiled to the Opcode code. Finally, the Opcode code is executed to return the result. Therefore, the Opcode code can be cached during the first running of the same php file, the next time you execute this page, you will find the cached opcode and execute the last step without any intermediate steps. XCache, Turck MM Cache, and PHP Accelerator are well-known. Tents (); ***** write the cached content to the html file ***** Ob_end_clean ();

The above is the php Tutorial: the content of the 9 Major cache technologies summary in PHP. For more information, see PHP Chinese network (www.php1.cn )!

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.