PHP Cache technology

Source: Internet
Author: User
The WEB program obtains information mainly by querying the database. when the database is not very large, there will be no major problems. however, with the development of the website, when the database grows in a geometric level, there will be bottlenecks. as a result, the PHP Cache technology gave birth to the WEB program to obtain information mainly by querying the database. when the database is not very large, there will not be too many problems. however, with the development of the website, when the database grows in a geometric level, there will be bottlenecks. so PHP Cache technology was born. when the PHP Cache technology works, when the program queries data, It serializes the corresponding results and saves them to the file. in the future, the same query statement can be used without directly querying the database, instead, it is obtained from the cache file. This improvement improves the program running speed.
Currently, the popular methods for using PHP Cache technology are gold partners such as Adodb and Smarty.
Working Principle of PHP Cache technology:
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 ('http://www.aboutstudy.net ', '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;
5if (! $ 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.
PHP, a web design scripting language that has emerged in recent years. due to its powerful and scalability, php has developed considerably in recent years. Compared with traditional asp websites, it has an absolute advantage in speed. if you want mssql to convert 60 thousand pieces of data to php, it will take 40 seconds, and asp will take less than 2 minutes. however, due to the increasing number of website data, we are eager to call data more quickly. We do not need to drop data from the database every time. we can choose from other places, such as a file, or a memory address, which is the Cache technology of php.
Related Article

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.