php檔案快取執行個體代碼_PHP教程

來源:互聯網
上載者:User
php教程檔案快取執行個體代碼
緩衝在實際使用當中應用很廣泛,可以減輕對伺服器資料庫教程的訪問,提高運行速度。目前很多cms內容管理系統中頻繁使用緩衝機制來提高系統啟動並執行效率
cache.php 代碼如下:
php代碼
/*
使用者需要事先定義的常量:
_cachepath_ 模板緩衝路徑
_cacheenable_ 自動緩衝機制是否開啟,未定義或為空白,表示關閉自動緩衝機制
_recachetime_ 自動重新緩衝間隔時間,單位為秒,未定義或為空白,表示關閉自動重新緩衝
*/

class cache {

var $cachefile;
var $cachefilevar;

function cache() {
//產生當前頁的cache組檔案名稱 $this->cachefilevar 及檔案名稱 $this->cachefile
//動態網頁的參數不同對應的cache檔案也不同,但是每一個動態網頁的所有cache檔案都有相同的檔案名稱,只是副檔名不同
$s=array(".","/");$r=array("_","");
$this->cachefilevar=str_replace($s,$r,$_server["script_name"])."_".$_get[_actionvar_];
$this->cachefile=$this->cachefilevar.".".md5($_server["request_uri"]);
}

//刪除當前頁/模組的緩衝
function delete() {
//刪除當前頁的緩衝
$d = dir(_cachepath_);
$strlen=strlen($this->cachefilevar);
//返回當前頁的所有cache檔案組
while (false !== ($entry = $d->read())) {
if (substr($entry,0,$strlen)==$this->cachefilevar) {
if (!unlink(_cachepath_."/".$entry)) {echo "cache目錄無法寫入";exit;}
}
}
}

//判斷是否已cache過,以及是否需要cache
function check() {
//如果設定了緩衝更新間隔時間 _recachetime_
if (_recachetime_+0>0) {
//返回當前頁cache的最後更新時間
$var=@file(_cachepath_."/".$this->cachefilevar);$var=$var[0];
//如果更新時間超出更新間隔時間則刪除cache檔案
if (time()-$var>_recachetime_) {
$this->delete();$ischage=true;
}
}
//返回當前頁的cache
$file=_cachepath_."/".$this->cachefile;
//判斷當前頁cache是否存在 且 cache功能是否開啟
return (file_exists($file) and _cacheenable_ and !$ischange);
}

//讀取cache
function read() {
//返回當前頁的cache
$file=_cachepath_."/".$this->cachefile;
//讀取cache檔案的內容
if (_cacheenable_) return readfile($file);
else return false;
}

//產生cache
function write($output) {
//返回當前頁的cache
$file=_cachepath_."/".$this->cachefile;
//如果cache功能開啟
if (_cacheenable_) {
//把輸出的內容寫入cache檔案
fopen($file,'w'">$fp=@fopen($file,'w');
if (!@fputs($fp,$output)) {echo "模板cache寫入失敗";exit;}
@fclose($fp);
//如果設定了緩衝更新間隔時間 _recachetime_
if (_recachetime_+0>0) {
//更新當前頁cache的最後更新時間
$file=_cachepath_."/".$this->cachefilevar;
$fp=@fopen($file,'w');
if (!@fwrite($fp,time())) {echo "cache目錄無法寫入";exit;}
@fclose($fp);
}
}
}

}
?>
使用過程:
php代碼
define("_cachepath_","./cache/");
define("_cacheenable_","1");
define("_recachetime_","43200");
include('cache.php');
$cache=new cache();
if ($cache->check()) {
$template=$cache->read();
}else {
ob_start();
ob_implicit_flush(0);
?>
頁面內容。。。。
$template = ob_get_contents();
$cache->write($template);
}
?>


http://www.bkjia.com/PHPjc/444788.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444788.htmlTechArticlephp教程檔案快取執行個體代碼 緩衝在實際使用當中應用很廣泛,可以減輕對伺服器資料庫教程的訪問,提高運行速度。目前很多cms內容管理系統...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.