Cache
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, my article in-depth discussion: talk about PHP to generate static pages , this is a way, but this is temporary, is 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
Class cache{
/*
class Name:cache
Description:control to cache data, $cache _out_time is a array t o Save Cache Date Time out.
version:1.0
Author: Old farmer Cjjer
last modify:2006-2-26
Author url:http://www.cjjer.com
*/
Private $ Cache_dir;
Private $expireTime =180;//cache time is 60 seconds
function __construct ($cache _dirname) {
if (! @is_dir ($cache _ dirname) {
if (! @mkdir ($cache _dirname,0777)) {
$this->warn (' cached file does not exist and cannot be created. Need to manually create. ');
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. = (!empty ($_server[' query_string '))? '?' . $_server[' query_string ']: ';
}
return $url;
}
function warn ($errorstring) {
echo "<b><font color= ' red ' > Error:<pre>". $errorstring. " </pre></font></b> ";
}
Function Cache_page ($pageurl, $pagedata) {
if (! $fso =fopen ($pageurl, ' W ')) {
$this-> Warns (' Unable to open cache file. '); /trigger_error
return false;
&NBSP}
if (!flock ($FSO, lock_ex)) {//LOCK_NB, exclusive lock
$this->warns (' Unable to lock cache file. '); /trigger_error
return false;
&NBSP}
if (!fwrite ($FSO, $pagedata)) {//write byte stream, serialize write to other format
$this->warns (' Unable to write cache file. '); /trigger_error
return false;
&NBSP}
flock ($fso, Lock_un);//Release lock
fclose ($FSO);
return true;
}
Function Display_cache ($cacheFile) {
if ( !file_exists ($cacheFile)) {
$this->warn (' Unable to read cache file. '); /trigger_error
return false;
}
echo ' reads cache file: '. $ Cachefile;
//return unserialize (file_get_contents ($cacheFile));
$fso = fopen ($cacheFile, ' R ');
$data = fread ($fso, FileSize ($cacheFile));
fclose ($FSO);
return $data;
}
function ReadData ($cacheFile = ' default_cache.txt ') {
$cacheFile = $this->cache_dir. " /". $cacheFile;
if (file_exists ($cacheFile) &&filemtime ($cacheFile) > (Time ()-$this->expiretime)) {
$data = $this->display_cache ($cacheFile);
}else{
$data = "From-here wo can-get-it-it from MySQL database,update-<b>". Date (' L DS \of F Y h:i:s A '). " </b>, the expiration date is: ". Date (' L DS \of F Y h:i:s A ', time () + $this->expiretime)." ----------";
$this->cache_page ($cacheFile, $data);
}
return $data;
}
}
?>
I'll break down this code to explain it line by row.
[1] [2] Next page