<?php
/*
self cacher v 1.0.1
need PHP v4.0.6 or above
by Hisun Yang (a Chinese)
@ 2002.01.09
yanghisun@english88.com.cn (accepts any questions )
caution: can't cache posted page
//run these command in linux
mkdir /tmp/cache
chown nobody:nobody cache
chmod 700 cache
//do these in windows
create a folder 'D:/Apache/tmp/cache' (your cached files will be there)
*/
function _self_cacher_getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
class TSelfCacher{
var $_log_performance=FALSE;
var $_log_full_filename;
var $_start_time;
//
var $_cache_to_dir="/tmp/cache/"; //linux
//var $_cache_to_dir="D:/Apache/tmp/cache"; //windows ,you must create this folder yourself
var $_cached_filename;
var $_cached_full_filename;
var $_cached_content;
//
//constructor
function TSelfCacher($ALogPerformance=FALSE){
//if $ALogPerformance ,enable performance log
$this->_log_performance=$ALogPerformance?TRUE:FALSE;
if($this->_log_performance) $this->_start_time=_self_cacher_getmicrotime();
global $HTTP_SERVER_VARS;
if($HTTP_SERVER_VARS["REQUEST_METHOD"]=="POST") return; //don't cache posted file
$this->setCacheFileAndDir();
$this->existOrMakeToDir();
if($this->getCachedContent()){
$this->show();
}else{
$this->startCache();
return $this;
}
}
function setCacheFileAndDir(){
global $HTTP_SERVER_VARS;
$this->_cached_filename=md5($HTTP_SERVER_VARS["REQUEST_URI"]).".cache";
$this->_cache_to_dir.="/".$HTTP_SERVER_VARS["SERVER_NAME"].'_'.date("Ymd");
$this->_cached_full_filename=$this->_cache_to_dir."/".$this->_cached_filename;
if($this->_log_performance){$this->_log_full_filename=$this->_cache_to_dir."/performance.log";}
}
function getCachedContent(){ //return true when get cached content before ,or return false
if(!file_exists($this->_cached_full_filename)){ return FALSE;}
if($fp=@fopen($this->_cached_full_filename,"r")){
flock($fp,LOCK_SH);
$this->_cached_content=fread($fp,filesize($this->_cached_full_filename));
flock($fp,LOCK_UN);
fclose($fp);
return TRUE;
}else{
return FALSE;
}
}
function setCachedContent(&$AContent){ //call function by ref can speed up
$this->_cached_content=$AContent;
}
function writeCachedContent(){
if($fp=@fopen($this->_cached_full_filename,"w")){
flock($fp,LOCK_EX);
fwrite($fp,$this->_cached_content);
flock($fp,LOCK_UN);
fclose($fp);
if($this->_log_performance){
$this->logToFile();
}
return TRUE;
}else{
return FALSE;
}
}
function logToFile(){
$_spent_time=_self_cacher_getmicrotime()-$this->_start_time;
if($fp=@fopen($this->_log_full_filename,"a")){
flock($fp,LOCK_EX);
fputs($fp,date("[Y-m-d H:i:s]").$this->_cached_full_filename." ".$_spent_time." microseconds\n");
flock($fp,LOCK_UN);
fclose($fp);
return TRUE;
}else{
return FALSE;
}
}
function startCache(){
ob_start("_cacher_call_back");
}
function existOrMakeToDir(){ //return true if is dir ,or make it
if(is_dir($this->_cache_to_dir)){
return TRUE;
}
else{
return @mkdir($this->_cache_to_dir,0700);
}
}
function show(){
echo $this->_cached_content;
if($this->_log_performance){
$this->logToFile();
}
exit();
}
function flush(){
ob_end_flush();
}
}
function _cacher_call_back(&$AContent){
global $_SELF_CACHER;
$_SELF_CACHER->setCachedContent($AContent);
$_SELF_CACHER->writeCachedContent();
return $_SELF_CACHER->_cached_content;
}
if(!isset($_SELF_CACHER))$_SELF_CACHER=&new TSelfCacher;
//CAUTION: the name _SELF_CACHER can't be modified
?> |