PHP Cache technology

Source: Internet
Author: User
Why does PHP Cache technology need to be used? The reason is simple: improve efficiency. In program development, the primary way to obtain information is to query the database. In addition, it may also be through Web Services or some other method. no matter which method, in the face of a large number of concurrent accesses, they may all become efficiency bottlenecks. to solve these problems, many solutions are proposed, some of which use optimization software (such as APC, Eaccelerator, Zend Optimizer, and so on) to improve the running efficiency of the program, the rational use of these software can often improve the running efficiency of the program by an order of magnitude, but the premise is that you must control 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 an optimization software, otherwise, you must use PHP to implement the corresponding cache function. If this makes you feel at a loss, I believe the text below can give you some inspiration.
Many PHP programmers use gold partners such as Adodb + Smarty, so they should first look at how to use their cache functions.
First, let's take a look at the data cache function provided by adodb:

1 2 include ('adodb. inc. php'); # load code common to adodb
3 $ ADODB_CACHE_DIR = '/usr/ADODB_cache ';
4 $ conn = & ADONewConnection ('mysql'); # create a connection
5 $ conn-> PConnect ('localhost', 'userid', '', 'agora '); # connect to MySQL, agora db
6 $ SQL = 'SELECT mermername, CustomerID from customer ';
7 $ rs = $ conn-> CacheExecute (15, $ SQL );
8?>

As shown above, each time data is queried, the corresponding results are serialized and saved to the file. in the future, the same query statement can be obtained from the cache file instead of directly querying the database.
Let's take a look at the page cache function provided by Smarty:

1 2 require ('smarty. class. php ');
3 $ smarty = new Smarty;
4 $ smarty-> caching = true;
5 if (! $ Smarty-> is_cached ('index. tpl ')){
6 // No cache available, do variable assignments here.
7 $ contents = get_database_contents ();
8 $ smarty-> assign ($ contents );
9}
10 $ smarty-> display ('index. tpl ');
11?>
12


As shown above, each time you access the page, the system first checks whether the corresponding cache exists. if it does not exist, it connects to the database and obtains the data. after the template variable value is assigned, the page is displayed, the cache file is generated at the same time, so that the cache file will play a role the next time you access it, instead of executing the if block data query statement. Of course, there are many things to consider in actual use, such as setting the validity period and cache group. for details, refer to the related sections on cache (caching) in the Smarty manual.
The two popular PHP component caching methods have different focuses. for the Adodb cache, it caches data and for the Smarty cache, it caches pages. There are many other components that provide the caching function (for example, PEAR: Cache_Lite). the specific analysis of the specific solution used in actual programming may also be used in combination.
One obvious advantage of using the built-in caching scheme of these components is that their implementation is transparent to the client. You only need to make the necessary settings (such as the cache time and cache directory), instead of having to think too much about the cache details, the system will automatically manage the cache according to the settings. However, its disadvantages are also obvious, because every request still needs to be parsed using PHP, and the efficiency is still greatly reduced compared with pure static, which still cannot meet the requirements in the face of large PVS. in this case, static caching is required because dynamic caching is not enough.
This article reprinted from the feet home: http://www.jb51.net/html/200611/67/4965.htm

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.