Cache
class_cache.php:
<?php
Class Cache
{
var $cacheDirectory;
var $cacheDuration = 3600;
var $cacheFilename;
function cache ($cacheDuration =3600, $cacheDirectory = './cache ')
{
$this->cacheduration = 0;
$this->cachedirectory = '. ';
$this->cachefilename = ';
$this->updatecache ($cacheDuration, $cacheDirectory);
}
function Getcachefilename ()
{
return $this->cachefilename;
}
function Updatecache ($cacheDuration =3600, $cacheFolder = './cache ')
{
$this->cacheduration = $cacheDuration;
$this->cachedirectory = $cacheFolder;
$this->_makecachefolder ();
}
function _makecachefolder ()
{
/*if (!is_dir ($this->cachedirectory))
{
$temp = explode ('/', $this->cachedirectory);
$cur _dir = ';
for ($i =0; $i <count ($temp); $i + +)
{
$cur _dir. = $temp [$i]. ' /';
if (!is_dir ($cur _dir))
{
if (@mkdir ($cur _dir,777) && ($cur _dir!=getcwd ())
{
$this->_writefile ($cur _dir. '. htaccess ', ' Deny from all ');
$this->_writefile ($cur _dir. ' index.html ');
}
}
}
}*/
if (!is_dir ($this->cachedirectory))
{
$cur _dir= $this->cachedirectory;
echo $cur _dir;
if (@mkdir ($cur _dir,777))
{
$this->_writefile ($cur _dir. '. htaccess ', ' Deny from all ');
$this->_writefile ($cur _dir. ' index.html ');
}
}
}
function _writefile ($filename, $contents)
{
if (!file_exists ($filename))
{
$fp = @fopen ($filename, ' w ');
if ($FP)
{
Fputs ($fp, $contents);
Fclose ($FP);
}
}else{
Unlink ($filename);
$fp = @fopen ($filename, ' w ');
if ($FP)
{
Fputs ($fp, $contents);
Fclose ($FP);
}
}
}
function _setcachefilename ($contents)
{
$this->cachefilename = $this->cachedirectory. /'. MD5 ($contents). TXT ';
/***********/
Global $cache _file;
$this->cachefilename = $this->cachedirectory. /'. $cache _file. TXT ';
/***********/
}
function Returncachetime ()
{
return "ASDFD";
$tim =filemtime ($this->cachefilename);
return date (' Y year m month D Day h when I minute s ', $tim);
}
function Incache ($contents, $sty = ')
{
$this->_setcachefilename ($contents);
if ($sty ==1)
{
Return file_exists ($this->cachefilename);
}else{
if (file_exists ($this->cachefilename))
{
$tim =filemtime ($this->cachefilename);
if ((Time ()-$tim) > $this->cacheduration)
{
return false;
}else{
return true;
}
}else{
return false;
}
}
}
function Readcache ()
{
$contents = ';
$fp = @fopen ($this->cachefilename, ' R ');
if ($FP)
{
while (!feof ($FP))
$contents. = Fread ($fp, 4096);
Fclose ($FP);
}
return $contents;
}
function Saveincache ($contents, $filename = ')
{
if (Trim ($filename) = = ") $filename = $contents;
if ($this->incache ($filename, 1))
{
if ((Time ()-filemtime ($this->cachefilename)) > $this->cacheduration)
{
@unlink ($this->cachefilename);
}
}
$this->_writefile ($this->cachefilename, $contents);
}
}
?>
cache.php:
?
Require_once ("class_cache.php");? >
?
---------Page Caching----------
$is whether _cache=1;//is cached
$cache _time=300;//Cache Time
if ((Strstr ($script _name, "/member/") = = True) | | (Strstr ($script _name, "/common/") = = True))
$is _cache=0;
$cacheDirectory =$_server[' Document_root ']. " /cache/";
if ($_server[' query_string ']== ')
$cache _file=$_server[' php_self '];
Else
$cache _file=$_server[' php_self ']. $_server[' query_string '];
if ($_server[' php_self ']== "/index.php")
$cache _file= "___index.php";
$cache _file=preg_replace (Array ("/\//", "/\?/"), Array ("", ""), $cache _file);
echo $cache _file;
if ($is _cache==1)
{
$cache =new Cache ($cache _time, $cacheDirectory);
if ($cache->incache ($cache _file))
{
$output = $cache->readcache ();
$CacheTime = $cache->returncachetime ();
Unset ($cache);
if (Function_exists (Return_execute_time ()))
$execute _time=return_execute_time ();
$output =str_replace ("<!--show_execute_time-->", $execute _time.) <br> cached version: ". $CacheTime, $output);
Print ($output);
Exit
}else
Ob_start ();
}
function All_cache ()
{
Global $is _cache;
Global $cache _file;
Global $cache;
if ($is _cache==1)
{
Here is the output of the content
$output = Ob_get_clean ();
Ob_end_clean ();
$cache->saveincache ($output, $cache _file);
$CacheTime = $cache->returncachetime ();
Unset ($cache);
if (Function_exists (Return_execute_time ()))
$execute _time=return_execute_time ();
$output =str_replace ("<!--show_execute_time-->", $execute _time.) <br> cached version: ". $CacheTime, $output);
Print ($output);
Exit
}
}
?>
Usage
Reference at the beginning of the page
?
Require ("cache.php")?>
At the end of the page, add
?
All_cache ();? >
Practical Application Http://www.scmetals.com
Class_cache class Original paste: http://www.phpx.com/happy/thr83014.html
class_cache.php content is as follows
<?php
Class Cache
{
var $cacheDirectory;
var $cacheDuration = 3600;
var $cacheFilename;
function cache ($cacheDuration =3600, $cacheDirectory = './cache ')
{
$this->cacheduration = 0;
$this->cachefilename = ';
$this->cachedirectory = '. ';
$this->updatecache ($cacheDuration, $cacheDirectory);
}
function _makecachefolder ()
{
if (!is_dir ($this->cachedirectory))
{
$temp = explode ('/', $this->cachedirectory);
$cur _dir = ';
for ($i =0; $i <count ($temp); $i + +)
{
$cur _dir. = $temp [$i]. ' /';
if (!is_dir ($cur _dir))
{
if (@mkdir ($cur _dir,777) && ($cur _dir!=getcwd ())
{
$this->_writefile ($cur _dir. '. htaccess ', ' Deny from all ');
$this->_writefile ($cur _dir. ' index.html ');
}
}
}
}
}
function Getcachefilename ()
{
return $this->cachefilename;
}
function _setcachefilename ($contents)
{
$this->cachefilename = $this->cachedirectory. /'. MD5 ($contents). TXT ';
}
function Incache ($contents, $sty = ')
{
$this->_setcachefilename ($contents);
if ($sty ==1)
{
Return file_exists ($this->cachefilename);
}
Else
{
if (file_exists ($this->cachefilename))
{
$tim =filemtime ($this->cachefilename);
if ((Time ()-$tim) > $this->cacheduration)
{
return false;
}
Else
{
return true;
}
}
Else
{
return false;
}
}
}
function Readcache ()
{
$contents = ';
$fp = @fopen ($this->cachefilename, ' R ');
if ($FP)
{
while (!feof ($FP)) $contents. = Fread ($fp, 4096);
Fclose ($FP);
}
return $contents;
}
function Updatecache ($cacheDuration =3600, $cacheFolder = './cache ')
{
$this->cacheduration = $cacheDuration;
$this->cachedirectory = $cacheFolder;
$this->_makecachefolder ();
}
function Saveincache ($contents, $filename = ')
{
if (Trim ($filename) = = ") $filename = $contents;
if ($this->incache ($filename, 1))
{
if ((Time ()-filemtime ($this->cachefilename)) > $this->cacheduration)
{
@unlink ($this->cachefilename);
}
}
$this->_writefile ($this->cachefilename, $contents);
}
function _writefile ($filename, $contents)
{
if (!file_exists ($filename))
{
$fp = @fopen ($filename, ' w ');
if ($FP)
{
Fputs ($fp, $contents);
Fclose ($FP);
}
}
Else
{
Unlink ($filename);
$fp = @fopen ($filename, ' w ');
if ($FP)
{
Fputs ($fp, $contents);
Fclose ($FP);
}
}
}
}
?>