PHP Caching Technology

Source: Internet
Author: User
Tags apc php compiler php 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:

Currently the most common PHP compiler caching tools are: Apc,accelerator,xcache (domestic) and so on.

PHP is an interpreted language that requires the following two steps when executing code in the PHP language:

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:

The data cache for PHP 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. 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.

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

Ob_start ()

The code to run *******

$content = Ob_get_contents ();

Write cached content to HTML file * * * *

Ob_end_clean ();

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.

PS: 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.

11. 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.

PHP 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.