Caching refers to the temporary file Exchange area, where computers put the most commonly used files out of storage and put them in the cache temporarily, just like putting tools and materials on a workbench, which is more convenient than going to the warehouse. Because the cache is often using RAM (power off that is not a permanent storage), so after busy still will send files to the hard disk and other storage storage permanent. The largest cache in the computer is the memory, the fastest is the CPU set on the L1 and L2 cache, video card memory is to the GPU cache, the hard disk also has 16M or 32M of cache. Do not understand the cache as a thing, it is a way of processing collectively!
The most effective way to cope with high traffic in web development is to use caching technology, which can effectively improve the load performance of the server and exchange space for time.
The internet is also the 2 8 verdict, just like the keyword in Baidu search, 80% of people Search is definitely 20% of the content, so only need to put the content of the 20% keyword Shou can be very effective in billions of of the record quickly return to the user needs content.
In this article, let's take a look at some of the Shou that are commonly used in PHP web development.
1. Universal Caching Technology:
Data caching: Here is the data cache refers to the database query PHP caching mechanism, each visit to the page, will first detect the corresponding cache data exists, if it does not exist, connect the database, get the data, and the query results serialized after saving to the file, Later, the same query results are obtained directly from the cached table or file.
The broadest example looks at the Discuz search function, caches the result ID in a table, and searches for the cached table the next time the same keyword is searched.
In a common way, when multiple tables are associated, the contents of the table to generate an array to save the main tables in a field, the need to decompose the array, the advantage is to read only a table, the disadvantage is that two data synchronization will be more than a few steps, the database is always a bottleneck, with hard disk for speed, is this key point.
2. Page Caching:
Every time you visit the page, you will first detect the corresponding cache page file exists, if it does not exist, connect the database, get the data, display the page and generate cached page files, so the next time the page file will play a role. (Template engine and some common online PHP caching mechanism classes usually have this feature)
3, Time trigger cache:
Check that the file exists and that the timestamp is less than the set expiration time, or update the cache if the timestamp of the file modification is greater than the current timestamp minus the expiration time.
4, the content triggers the cache:
Forces an update of the PHP caching mechanism when data is inserted or updated.
5. Static caching:
Static caching here refers to static, directly generate HTML or XML and other text files, there are updates to regenerate once, suitable for the page not too changed, this is not said.
The above content is the code level solution, I direct CP other framework, also lazy to change, the content is similar, very easy to do, and will use several ways together, but the following content is the server side of the cache scheme, not code-level, to have a lot of cooperation to do
6, Memory cache:
Memcached is a high-performance, distributed memory object PHP caching mechanism system, used in dynamic applications to reduce database load, improve access speed.
7, the PHP buffer:
There are eaccelerator, APC, Phpa,xcache, this this will not say it, search a pile of a bunch of, you see, know that there is this thing OK
8, MySQL cache:
This is not a code level, the classic database is used in this way, look at the following running time, 0.09xxx and so on
9. Web caching based on reverse proxy:
such as Nginx,squid,mod_proxy (apache2 above and divided into Mod_proxy and Mod_cache)
10. DNS Polling:
Bind is an open source DNS server software, this to say is big, own search go, we know this thing on the line.
I know that there are chinacache and other stations is to do so, said the simple point is multiple servers, the same page or file cache to different servers, by North-South automatic resolution to the relevant server.
Why use caching technology? The reason is simple: improve efficiency. In program development, the main way to get information is to query the database, in addition, it may be through web services or some other way, either way, in the face of a large number of concurrent access, they can become the bottleneck of efficiency, in order to solve these problems, people put forward a lot of solutions, Some of them are the use of optimization software (such as: Apc,eaccelerator,zend optimizer, etc.) to improve the operating efficiency of the program, the rational use of these software, often can make the operation of the program efficiency of the number of upgrade, but the prerequisite is that you must have control of the host, So that you can install the software, if you are using a virtual host, you can only pray that your service provider has pre-installed a certain optimization software, otherwise you must use PHP to implement the appropriate caching functions. If this makes you feel at a loss, believe that the following words can give you some inspiration.
Many PHP programmers use gold partners such as Adodb+smarty, so let's look at how to use their caching capabilities.
First look at the data caching capabilities provided by ADODB:
<?php
Include (' adodb.inc.php '); # Load code common to ADOdb
$ADODB _cache_dir = '/usr/adodb_cache ';
$conn = &adonewconnection (' mysql '); # Create a connection
$conn->pconnect (' localhost ', ' userid ', ', ', ' Agora '); # Connect to MySQL, Agora DB
$sql = ' Select CustomerName, CustomerID from customers ';
$rs = $conn->cacheexecute ($sql);
?>
As above, each time the data is queried, the corresponding results are serialized and saved to the file, and the same query statement can be obtained from the cached file instead of querying the database directly.
Let's look at the page caching features provided by Smarty:
<?php
Require (' Smarty.class.php ');
$smarty = new Smarty;
$smarty->caching = true;
if (! $smarty->is_cached (' Index.tpl ')) {
No cache available, do variable assignments here.
$contents = Get_database_contents ();
$smarty->assign ($contents);
}
$smarty->display (' Index.tpl ');
?>
As above, every time you visit the page, will first detect the existence of the corresponding cache, if it does not exist, connect the database, get data, complete the assignment of template variables, display pages, and generate cached files, so the next time the cache file will play a role, and no longer execute if block data query statements. Of course, there are a lot of things to consider in actual use, such as setting the expiration date, setting up the cache group, and so on, to see the relevant chapters in the Smarty Manual for Caching (caching).
The above two PHP popular component caching methods focus on different, for the ADODB cache, it is cached data, for Smarty cache, it is cached pages. There are many other components that provide caching functionality, such as Pear::cache_lite, and so on, which scenarios are used in actual programming for specific analysis, and may be used in combination.
One obvious benefit of using cache schemes built into these components is that their implementations are transparent to the client. As long as the necessary settings (such as: Cache time, cache directory, and so on) can be, without too much consideration of the implementation of the cache details, the system will automatically manage the cache based on settings. But its shortcomings are also obvious, because each request will still be used to parse the PHP, efficiency and pure static compared with the big discount, in large PV still can not meet the requirements, in this case, just do dynamic caching is not enough, must implement static caching.
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