一個不錯php檔案快取類檔案_PHP教程

來源:互聯網
上載者:User
本人給大家推一個不錯php檔案快取類檔案,從各方面來看本緩衝類很合理並且適用於大型網站使用哦,有需要的朋友可參考參考。
代碼如下 複製代碼

class Cache {
/** 緩衝目錄 **/
var $CacheDir = './c';
/** 緩衝的檔案 **/
var $CacheFile = '';
/** 檔案快取時間(分鐘) **/
var $CacheTime = 0;
/** 檔案是否已緩衝 **/
var $CacheFound = False;
/** 錯誤及調試資訊 **/
var $DebugMsg = NULL;

function Cache($CacheTime = 0) {
$this->CacheTime = $CacheTime;
}

private function Run() {
/** 緩衝時間大於0,檢測快取檔案的修改時間,在緩衝時間內為快取檔案名,超過緩衝時間為False,
小於等於0,返回false,並清理已緩衝的檔案
**/
Return $this->CacheTime ? $this->CheckCacheFile() : $this->CleanCacheFile();
}
function GetCache($VistUrl,$CacheFileType = 'html')
{
$this->SetCacheFile($VistUrl,$CacheFileType);

$fileName=$this->CheckCacheFile();
if($fileName)
{
$fp = fopen($fileName,"r");
$content_= fread($fp, filesize($fileName));
fclose($fp);
return $content_;
}
else
{
return false;
}
}
private function SetCacheFile($VistUrl,$CacheFileType = 'html') {
if(empty($VistUrl)) {
/** 預設為index.html **/
$this->CacheFile = 'index';
}else {
/** 傳遞參數為$_POST時 **/
$this->CacheFile = is_array($VistUrl) ? implode('.',$VistUrl) : $VistUrl;
}
$this->CacheFile = $this->CacheDir.'/'.md5($this->CacheFile);
$this->CacheFile.= '.'.$CacheFileType;
}

function SetCacheTime($t = 60) {
$this->CacheTime = $t;
}

private function CheckCacheFile() {
if(!$this->CacheTime || !file_exists($this->CacheFile)) {Return False;}
/** 比較檔案的建立/修改日期和當前日期的時間差 **/
$GetTime=(Time()-Filemtime($this->CacheFile))/(60*1);
/** Filemtime函數有緩衝,注意清理 **/
Clearstatcache();
$this->Debug('Time Limit '.($GetTime*60).'/'.($this->CacheTime*60).'');
$this->CacheFound = $GetTime <= $this->CacheTime ? $this->CacheFile : False;
Return $this->CacheFound;
}

function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') {
$this->SetCacheFile($VistUrl,$CacheFileType);
if(!$this->CacheTime) {
Return False;
}
/** 檢測緩衝目錄是否存在 **/
if(true === $this->CheckCacheDir()) {
$CacheFile = $this->CacheFile;
$CacheFile = str_replace('//','/',$CacheFile);
$fp = @fopen($CacheFile,"wb");
if(!$fp) {
$this->Debug('Open File '.$CacheFile.' Fail');
}else {
if(@!fwrite($fp,$Content)){
$this->Debug('Write '.$CacheFile.' Fail');
}else {
$this->Debug('Cached File');
};
@fclose($fp);
}
}else {
/** 緩衝目錄不存在,或不能建立目錄 **/
$this->Debug('Cache Folder '.$this->CacheDir.' Not Found');
}
}

private function CheckCacheDir() {
if(file_exists($this->CacheDir)) { Return true; }
/** 儲存當前工作目錄 **/
$Location = getcwd();
/** 把路徑劃分成單個目錄 **/
$Dir = split("/", $this->CacheDir);
/** 迴圈建立目錄 **/
$CatchErr = True;
for ($i=0; $i if (!file_exists($Dir[$i])){
/** 建立目錄失敗會返回False 返回建立最後一個目錄的傳回值 **/
$CatchErr = @mkdir($Dir[$i],0777);
}
@chdir($Dir[$i]);
}
/** 建立完成後要切換到原目錄 **/
chdir($Location);
if(!$CatchErr) {
$this->Debug('Create Folder '.$this->CacheDir.' Fail');
}
Return $CatchErr;
}

private function CleanCacheFile() {
if(file_exists($this->CacheFile)) {
@chmod($this->CacheFile,777);
@unlink($this->CacheFile);
}
/** 置沒有快取檔案 **/
$this->CacheFound = False;
Return $this->CacheFound;
}

function Debug($msg='') {
if(DEBUG) {
$this->DebugMsg[] = '[Cache]'.$msg;
}
}

function GetError() {
Return empty($this->DebugMsg) ? '' : "
n".implode("
n",$this->DebugMsg);
}
}/* end of class */


?>


http://www.bkjia.com/PHPjc/444619.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444619.htmlTechArticle本人給大家推一個不錯php檔案快取類檔案,從各方面來看本緩衝類很合理並且適用於大型網站使用哦,有需要的朋友可參考參考。 代碼如下...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.