ホームページ >php教程 >php手册 >キャッシュクラスの実践的な応用

キャッシュクラスの実践的な応用

WBOY
WBOYオリジナル
2016-06-21 09:05:501143ブラウズ

キャッシュ

Class_Cache.php:

クラスキャッシュ
{

var $cacheDirectory;

var $cacheDuration=3600;

var $cacheFilename;

関数キャッシュ($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{
$cur_dir .= $temp[$i].'/';

if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'. htaccess','すべてから拒否');
$this->_writeFile($cur_dir.'index.html','');
}
}
}
}*/
if (!is_dir($this->cacheDirectory))
{
$cur_dir=$this->cacheDirectory;
//エコー $cur_dir;
if (@mkdir($cur_dir,777))
{
$this->_writeFile($cur_dir.'.htaccess','すべて拒否');
$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';

/***********/
グローバル $cache_file;
$this->cacheFilename = $this->cacheDirectory.'/'.$cache_file.'.txt';
/***********/
}
function returnCacheTime()
{
//return "asdfd";
$tim=filemtime($this->cacheFilename);
return date('Y年m月d日H時i分秒',$tim);
}
関数 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{
true を返します。
}
}else{
false を返す;
}
}
}

function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp))
$contents .= fread($fp,4096);
fclose($fp);
}
$contents を返す;
}

function saveInCache($contents,$filename='')
{
if (trim($filename)=='') $filename = $contents;
if ($this->inCache($filename,1))
{
if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
{
@unlink ($this->キャッシュファイル名);
}
}
$this->_writeFile($this->cacheFilename,$contents);
}

}
?>
cache.php:
require_once("Class_Cache.php");?>
//-----------页面缓存----------
$is_cache=1;//否か缓存
$cache_time=300;//缓存続時間
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);
// $cache_file をエコー;

if($is_cache==1)
{
$cache=新しいキャッシュ($cache_time,$cacheDirectory);

if($cache->incache($cache_file))
{
$output=$cache->readcache();
$CacheTime=$cache->returnCacheTime();
設定解除($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace("",$execute_time."
缓存版本:".$CacheTime,$output);
print($output);
exit;
}else
ob_start();
 
}
function all_cache()
{
global $is_cache;
global $cache_file;
global $cache;
if($is_cache==1)
{
//这里是输出的内容
 
$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("",$execute_time."
缓存版本:".$CacheTime,$output);
print($output);
//exit; 

}
?>
用法
在页面开头引用
require("cache.php")?>
在页面最后加上
all_cache();?>

实际应用http://www.scmetals.com
class_cache类原文:http://www.phpx.com/happy/thr83014.html
class_cache.php内容下記

クラスキャッシュ
{

var $cacheDirectory;

var $cacheDuration=3600;

var $cacheFilename;

関数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->cacheディレクトリ);
$cur_dir = '';
for($i=0;$i {
$cur_dir .= $temp[$i].'/';
if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'.htaccess ','全員から拒否');
$this->_writeFile($cur_dir.'index.html','');
}
}
}

}

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->キャッシュファイル名);
if((time()-$tim)>$this->cacheDuration)
{
falseを返します。
}
                 else
{
trueを返します。
}
}
else
{
falseを返します。
}
}
}

function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp)) $contents .= fread($fp,4096);
fclose($fp);
}
$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->キャッシュファイル名);
}
}
$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);
             }
}
}

}
?>



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。