Pinggui and Wang Baochuan drama synopsis Introduction of PHP Cache technology

Source: Internet
Author: User
Tags apc php web development
The cache refers to the temporary file Exchange area, the computer put the most commonly used files from memory to put in the cache temporarily, just like the tools and materials on the workbench, this will be more convenient than the time to go to the warehouse. Because the cache tends to use RAM (a non-permanent storage that loses power off), the file is sent to the hard disk and so on for permanent storage after the work is done. The largest cache in the computer is the memory, the fastest is the CPU mounted L1 and L2 cache, graphics card memory is for the GPU cache, the hard disk also has 16M or 32M cache. Do not think of the cache as a thing, it is a way to deal with the collective!
The most effective way to cope with high traffic in web development is to use caching technology to effectively improve server load performance and space in exchange for time.
The Internet is also 2 8 conclusion, like Baidu search in the keyword, 80% of the people search is definitely 20% of the content, so only need to put this 20% of the key words of the contents of the Shou stored well can be very effective in billions of records quickly return to the user's content.
In this article, let's take a look at some of the Shou methods commonly used in PHP web development.
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.
Why use caching technology? The reason is simple: improve efficiency. In the process of development, the main way to obtain information is to query the database, in addition, may be through web services or some other method, 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 these are the use of optimization software (such as: Apc,eaccelerator,zend optimizer, etc.) to improve the operation of the program efficiency, reasonable use of these software, often can make the program's operational efficiency is increased in order to improve, but only if you have to host control, In order to be able to install the software, if you are using a virtual host, then you can only pray that your service provider has pre-installed an optimization software, otherwise you have to use PHP to implement the corresponding caching function. If this makes you feel at a loss, believe that the following words can give you some inspiration.
Many PHP programmers use Adodb+smarty as a golden partner, so look first at how to use their caching capabilities.
First look at the data caching capabilities provided by ADODB:
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 can be obtained from the cache file instead of querying the database directly.
Take a look at the page caching features provided by Smarty:
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, each visit to the page, will first detect the corresponding cache is present, if not, connect to the database, get the data, complete the assignment of template variables, display the page, and generate the cache file, so that the next time the cache file to play a role, and no longer execute if block data query statement. Of course, there are a lot of things to consider in actual use, such as the setting of the expiration date, the settings of the cache group, and so on, to see the relevant chapters of the Smarty Manual for Caching (caching).
The focus of the above two popular PHP component caching methods is different, for ADODB cache, it caches the data, for the Smarty cache, it caches the page. There are many other components that provide caching functionality (such as pear::cache_lite, etc.), which scenarios to use in real-world programming can be analyzed in detail and may be used in a comprehensive context.
One of the obvious benefits of using the caching 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 done, without too much consideration to implement the cache details, the system will automatically manage the cache according to the settings. But its shortcomings are equally obvious, because each request still to be parsed with PHP, efficiency and pure static compared to the big discount, in the large PV still can not meet the requirements, in this case, just do the dynamic cache is not enough, you must implement a static cache.

The above introduced the Pinggui and Wang Baochuan drama Synopsis PHP Cache technology Introduction, including the Pinggui and Wang Baochuan drama synopsis content, hope to PHP tutorial interested friends helpful.

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