PHP Web Cache Technology

Source: Internet
Author: User

Personal summary:

Static front-end: parses dynamic pages and saves them as static pages

File Cache: Save the query result as a file, XML

Memory Cache: memcache

PHP caches: xcache, eaccelerator, etc.

Memcache is a high-performance distributed memory object cache system. By maintaining a unified and huge hash table in the memory, memcache can be used to store data in various formats, including image, video, file, and database retrieval results. Simply put, the data is called to the memory and then read from the memory, which greatly improves the reading speed. Memcache is a danga project. It was originally livejournal.
The service was initially developed to accelerate livejournal access, and later used by many large websites. Memcached runs on one or more servers as a daemon and receives client connections and operations at any time.

Xcache is an open-source opcode Cache/optimizer, which means it can improve the PHP performance on your server. by caching the compiled PHP Data to the shared memory, he can avoid repeated compilation processes and directly use the compiled code in the buffer to improve the speed. generally, it can increase the page generation rate by 2 to 5 times, reducing the server load.

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

1. General Cache Technology:

Data Cache: The data cache here refers to the PHP cache mechanism for database queries. Each time you access the page, it first checks whether the cache data exists. If it does not exist, it connects to the database, obtain the data and serialize the query results and save them to the file. The same query results will be obtained directly from the cache table or file.

The most widely used example is the discuz search function. The result ID is cached in a table and the table is searched in the cache next time when the same keywords are searched.

For example, when multiple tables are joined, an array generated in the appendix is saved to a field in the master table. If necessary, the array is decomposed, the advantage is read-only tables. The disadvantage is that two data synchronization steps are many more steps, and the database is always the bottleneck. Changing the disk speed is the key point.

2. Page cache:

Each time you access the page, the system first checks whether the corresponding cached page file exists. If it does not exist, it connects to the database to obtain data, displays the page, and generates cache page files at the same time, in this way, the page file will play a role in the next visit. (The template engine and some common PHP cache mechanism classes on the Internet usually have this function)

3. cache triggered by Time:

  Check whether the file exists and the timestamp is earlier than the set expiration time. If the modified timestamp of the file is greater than the current timestamp minus the expiration timestamp, use the cache; otherwise, update the cache.

4. Content-triggered cache:

  When data is inserted or updated, the PHP cache is forcibly updated.

5. Static cache:

The static cache mentioned here refers to static, which directly generates HTML, XML, and other text files. It is re-generated once when there is an update. This is suitable for pages that do not change much.

The above content is a code-level solution. I am not too lazy to change the content of the CP framework. The content is similar and easy to implement. It can be used together in several ways, however, the following content is a server-side cache solution. Non-code-level solutions can only be achieved through multi-party cooperation.

6. Memory Cache:

Memcached is a high-performance, distributed memory object PHP cache mechanism system that reduces database loads in dynamic applications and improves access speed.

7. php Buffer:

  There are eaccelerator, APC, PHPA, and xcache. You don't need to talk about this. You can search for a bunch of them and read them by yourself. If you know what it is, OK.

8. MySQL cache:

This is not code-level. The classic database uses this method. See the following running time, such as 0.09xxx.

9. Reverse Proxy-based Web Cache:

  For example, nginx, squid, mod_proxy (apache2 and above are also divided into mod_proxy and mod_cache)

10. DNS round robin:  
BIND is an open-source DNS server software, which is too big to be mentioned. You can search for it by yourself.
I know that chinacache and other major sites are doing this. Simply put, they are multiple servers and cache the same page or file on different servers, it is automatically resolved to the relevant server by North and South.

PHP Web Cache instance

 

Ob_start (): indicates the Start mark of the page cache. The content of this function is stored in the page cache until ob_end_flush () or ob_end_clean;
Ob_get_contents (): used to obtain the content in the page cache. After obtaining the content, we can do whatever we want to do. We can filter fields and match the content ~~~ :)
Ob_end_flush (): indicates that the page cache ends. After my verification, the cached content is output to the current page, that is, the cached content can be displayed.
With these three PHP functions, You can implement powerful functions. If the database query volume is large, you can use cache to solve this problem.

First, set the expiration time. If the cached file needs to expire in 2 hours, you can set cache_time to 3600*2. You can use filectime () to obtain the cache file creation time (or filemtime () obtain the modification time). If the current time and file creation time exceed the specified expiration time, you can use the above three functions to retrieve data from the database first, then start caching ob_start () and write the HTML code of the page to be generated in the cache. After the cache is complete, get the cached content through ob_get_contents, then, use fwrite to write the cached content to the static page HTML.

If it does not expire, you can directly read the static page in the cache to avoid a large number of database access.

<? PHP $ _ time = 10; $ dir = "d :\\ PHP \"; function cache_start ($ _ time, $ DIR) {$ cachefile = $ dir. '/timeout'; $ cachetime =$ _ time; ob_start (); If (file_exists ($ cachefile) & (Time ()-filemtime ($ cachefile) <$ cachetime )) {include ($ cachefile); ob_end_flush (); exit ;}} function cache_end ($ DIR) {$ cachefile = $ dir. '/'.sha1(1__server}'request_uri'{}.'.html'; $ fp = fopen ($ cachefile, 'w'); f Write ($ FP, ob_get_contents (); fclose ($ FP); ob_end_flush ();} cache_start ($ _ time, $ DIR); // The output content is as follows, place it between cache_start and cache_end for ($ I = 0; $ I <5; $ I ++) {echo $ I; sleep (1 );} cache_end ($ DIR);?>

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.