In-depth exploration of PHP caching principles and use

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags address analysis asp cache caching compared compared to the data

PHP because of its power and scalability, in recent years have been considerable development, PHP compared to the traditional ASP site, there is an absolute advantage in speed, want to mssql transfer 60,000 data PHP If it takes 40 seconds, ASP no more than 2 However, due to the increasing number of data sites, we are eager to call data faster, not necessary every time from the database, we can from other places, such as a file, or a memory address, which is PHP caching technology, which is Cache technology.

In-depth analysis

In general, the purpose of the cache is to put the data in one place to make the visit faster, no doubt, the memory is the fastest, but hundreds of M of data can go to memory? This is not realistic, of course, Occasionally put temporary server cache, such as ob_start () the cache page is opened, then send the file header before the page contents are cached in memory, know the other page output automatically clear or wait for ob_get_contents return or ob_end_clean display cleared , Which can be well used in the generation of static pages, can be well reflected in the template.

In addition, there is an object in the ASP application, you can save the public parameters, which also count the point buffer, but in PHP, I have not seen the developer to produce such an object, indeed, no need .ASP.NET page caching technology The viewstate is used, and the cache is the file association, (not necessarily accurate), the file is modified, update the cache, the file has not been modified and does not timeout (Note 1), read the cache, the result is the train of thought, look Look at this source:

<? Php class cache {private $ cache_dir; private $ expireTime = 180; // The cached time is 60 seconds function __construct ($ cache_dirname) {if (@@_dir ($ cache_dirname)) {if (! @ Mkdir ($ cache_dirname , 0777)) {$ this-> warn ('cache file does not exist and can not be created and needs to be manually created.'); Return false;}} $ this-> cache_dir = $ cache_dirname;} function __destruct () {echo 'Cache class bye ';} function get_url () {if (! isset ($ _ SERVER [' REQUEST_URI '])) {$ url = $ _SERVER [' REQUEST_URI '];} else {$ url = $ _SERVER [' SCRIPT_NAME '] $ url. = (! emptyempty ($ _ SERVER ['QUERY_STRING']))? '?'. $ _SERVER ['QUERY_STRING']: '';
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.