PHP Web Caching technology

Source: Internet
Author: User

Foreground static: Save the dynamic page as a static page after parsing

File caching: Save query results as files, XML

Memory Cache: Memcache

PHP cache: XCache, eaccelerator, etc.

Memcache is a high-performance distributed memory object caching system that can be used to store data in a variety of formats, including images, videos, files, and database retrieval results, by maintaining a unified, huge hash table in memory.  The simple thing is to call the data into memory and then read it from memory, which greatly improves the reading speed.  Memcache is a project of Danga, the first LiveJournal service, originally developed in order to speed up LiveJournal access, was later adopted by many large-scale websites. Memcached is a daemon that runs on one or more servers and receives client connections and operations at any time.

XCache is an open source opcode cache/optimizer, which means that he can improve PHP performance on your server. By buffering the compiled PHP data to shared memory to avoid a repetitive compilation process, he is able to use the buffer-compiled code directly to speed up. You can typically increase your page generation rate by 2 to 5 times times, reducing server load.

*************************************************************************************************************** ***************************************

1. Universal Cache 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.

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.

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 usually have this feature)

3. Time-Triggered cache:

  Check that the file exists and that the timestamp is less than the expiration time of the setting, and if the timestamp of the file modification is greater than the current timestamp minus the expiration timestamp, then cache is used, otherwise the cache is updated.

4. Content Trigger Cache:

  Forces the PHP cache mechanism to be updated when data is inserted or updated.

5. Static cache:

Static caching refers to static, directly generated HTML or XML and other text files, there is an update when re-generated once, suitable for the less changing pages, this does not say.


The above content is a code-level solution, I direct CP other framework, too lazy to change, the content is almost, very easy to do, and will be used together in several ways, but the following content is the server-side caching scheme, non-code level, to have multi-party cooperation to do

6. Memory Cache:

Memcached is a high-performance, distributed memory object PHP caching mechanism system for reducing database load and increasing access speed in dynamic applications.

7, the buffer of PHP:

  There are eaccelerator, APC, Phpa,xcache, this is not to say, search a bunch of a bunch of, see for yourself, know that this thing is OK

8. mysql Cache:

This is also non-code level, the classic database is used in this way, look at the following running time, 0.09xxx and the like

9. Reverse proxy-based Web caching:

  such as Nginx,squid,mod_proxy (Apache2 and above are divided into mod_proxy and Mod_cache)

10. DNS Polling:
Bind is an open source DNS server software, this to say it is big, their own search, you know there is this thing on the line.
I know that there are chinacache and other major stations to do so, said the simple point is a multi-server, the same page or file cache to different servers, according to the North-South automatic resolution to the relevant server.

PHP Web Cache instance


Ob_start (): The start of the page cache flag, the function of the content until Ob_end_flush () or Ob_end_clean () are saved in the page cache;
Ob_get_contents (): Used to get the content in the page cache, get to the future, we can think how to deal with these content is OK, filter field, match content, can ~ ~ ~:)
Ob_end_flush (): Indicates the end of the page cache. And I verified that the cached content will be output to the current page, that is, the cache content can be displayed.
With these three PHP functions, you can implement powerful functions. If the database query volume is large, you can use the cache to solve this problem.

First, set the expiration time, if the cache file is required to expire 2 hours, you can set Cache_time to 3600*2, through Filectime () to get the cache file creation time (or filemtime () get the modified time), If the current time and file creation time exceeds the qualified expiration time, you can use the above three functions, first remove the data from the database, then start caching Ob_start (), and then write the HTML code of the page to be generated in the cache, after the end of the cache through Ob_get_contents ( Gets the contents of the cache and writes the cached content to the static page HTML via fwrite.

If it does not expire, read the static pages in the cache directly, avoiding a lot of database access.

View Plain
    1. <?php
    2. $_time = 10;
    3. $dir = "d:\\php\\";
    4. function Cache_start ($_time, $dir)
    5. {
    6. $cachefile = $dir. '/'. SHA1 ($_server[' Request_uri '). HTML ';
    7. $cachetime = $_time;
    8. Ob_start ();
    9. if (file_exists ($cachefile) && (Time ()-filemtime ($cachefile) < $cachetime))
    10. {
    11. Include ($cachefile);
    12. Ob_end_flush ();
    13. Exit
    14. }
    15. }
    16. function Cache_end ($dir)
    17. {
    18. $cachefile = $dir. '/'. SHA1 ($_server[' Request_uri '). HTML ';
    19. $fp = fopen ($cachefile, ' w ');
    20. Fwrite ($FP, ob_get_contents ());
    21. Fclose ($FP);
    22. Ob_end_flush ();
    23. }
    24. Cache_start ($_time, $dir);
    25. The following is the output, placed between Cache_start and Cache_end two methods
    26. for ($i =0; $i <5; $i + +)
    27. {
    28. echo $i;
    29. Sleep (1);
    30. }
    31. Cache_end ($dir);
    32. ?>

PHP Web Caching technology

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.