PHP Caching Technology

Source: Internet
Author: User

First, Introduction

PHP, a web design scripting language that has sprung up in recent years, because of its strong and scalable, in recent years has been a significant development, compared to traditional PHP ASP Web site, in the speed has an absolute advantage, want to MSSQL to 60,000 data PHP if required 40 seconds, ASP less than 2 minutes. But, With more and more data on the web, we crave faster invocation of data without having to drop every time from the database, from other places, like a file, or a memory address, which is the caching technology of PHP, the cache technology.

Second, in-depth analysis

Generally speaking, the purpose of caching is to put the data in a place to allow access to the faster point, no doubt, memory is the fastest, but, hundreds of m of data can be stored inside it? This is unrealistic, of course, sometimes temporarily such as server caching, such as Ob_start () This cached page opens the page content is cached in memory before sending the file header, knowing that the output of the page is automatically clear or waiting for the ob_get_contents to return, or the Ob_end_clean display of the purge, which can be used in the static page generation. In the template can be very good embodiment, this is a way, but this is temporary, not a good way to solve our problems.

In addition, there is an object in the ASP application, you can save the common parameters, which also count point cache, but in PHP, I still do not see the developer output this object, indeed, not necessary. ASP.net page caching technology is viewstate, and cache is file association , (not necessarily accurate), the file was modified, update the cache, the file has not been modified and does not time out (note 1), read the cache, return the result, is this idea, look at this source:


Php:
Warn (' Cached files do not exist and cannot be created, need to be created manually. ');
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[' scrīpt_name '];
$url. = (!empty ($_server[' query_string '))? ''?'' . $_server[' query_string ']: ';
}

return $url;
}

function warn ($errorstring) {
echo "Error occurred:

". $errorstring."

";
}

function Cache_page ($pageurl, $pagedata) {
if (! $fso =fopen ($pageurl, ' W ')) {
$this->warns ("Cannot open the cached file."); /trigger_error
return false;
}
if (!flock ($fso, lock_ex)) {//LOCK_NB, exclusive lock
$this->warns (' cannot lock cache file. '); /trigger_error
return false;
}
if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format
$this->warns ("Cannot write cache file."); /trigger_error
return false;
}
Flock ($fso, Lock_un);/release lock
Fclose ($FSO);
return true;
}

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.