Summary of php Cache technology-PHP Tutorial

Source: Internet
Author: User
Summary of php Cache technology. Summary of php Cache Technology 1. static full-page caching, that is, generating all pages into html static pages, which are directly accessed by users during access, instead of taking the php server's summary about the php Cache technology

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. In this way, in the CMS system

Is common, such as dedecms;

A common implementation method is to use the output cache:

Ob_start ()

* ***** The code to run *******

$ Content = Ob_get_contents ();

***** Write cached content to an html file *****

Ob_end_clean ();

2. partial page cache

In this way, static cache is performed for the parts that are not frequently changed on a page, but the blocks that are frequently changed are not cached and finally assembled and displayed together.

Ob_get_contents. you can also use a page fragment cache policy like ESI to cache relatively static parts of dynamic pages.

(For ESI technology, refer to baidu for details here ).

This method can be used, for example, on the product page in the mall;

3. data cache

As the name suggests, it is a way to cache data. for example, when a product information in the mall is requested by the product id, it will produce information including shop information and product information.

In this case, you can cache the data to a php file. the file name contains the product id to create a unique identifier. The next time someone wants to view the product

Directly call the information in this file, instead of querying the database. In fact, the cached file contains a php array;

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, it will be straight

First, call the data from the file, and the database will not be queried. However, the cache file name here may need to be based on the query statement to create a unique identifier;

Cache by time change

In fact, this is not a real cache method; the above 2, 3, 4 cache technology generally uses the time change judgment; that is, you need to set an effective

Time. during this validity period, the content of the cached file will be obtained for the same access. However, if the specified cache time is exceeded, the data needs to be retrieved from the database again,

And produce the latest cached files;

For example, I set the homepage of our mall 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 to update the cache file at the same time. when the buyer accesses the product information, the actual access is

A static page without accessing the database;

Yes. if the product page is not cached, you need to check the database every time you access a product. if 0.1 million people browse the product online, the server is under heavy pressure;

6. memory cache

Memcached is a high-performance distributed memory cache server. The general purpose is

The database query results reduce the number of database accesses 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 = '192. 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); // get information
?>

7. apache Cache module

After apache is installed, it cannot be cached. If the external cache or squid server requires web acceleration, you need to set it in htttpd. conf.

To activate the mod_cache module when installing apache.
When installing apache:./configure -- enable-cache -- enable-disk-cache -- enable-mem-cache

8. php APC cache extension

Php has an APC cache extension, which is php_apc.dll in windows. you need to load this module first and then 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; maximum memory consumed by each PHP page. the default value is 8 M.

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 and the result is returned. Therefore, for the same php file, the first running

You can cache the Opcode. The next time you execute this page, you will find the opcode under the cache and execute the last step without any intermediate steps.

XCache, Turck MM Cache, and PHP Accelerator are well-known;

Lifecycle 1. static cache of all pages, that is, static html pages are generated on all pages. when users access static pages, they will not go to the php server...

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.