-
-
/* $cache = new Cache("../cache/",20); // 建構子,建立快取類別物件
- $cache->PutCache(); // 倒出快取
- */
- class Cache
- {
- private $CacheDir = 'Cache'; /* 快取目錄*/
- private $SetTimeOut = 10; /* 快取過期時間*/
- private $SetExt = '.cache'; /* 快取檔案後綴名稱*/
- private $CacheFileUrl = ''; /*快取檔案所在位址*/
- private $CacheConfigFile = ''; /* 快取檔案設定資訊*/
public $LastUnixTimePoke = 0; /* 上一次快取的Unix 時間戳* /
- public $CurrentUnixTimePoke = 0;/* 目前快取的Unix 時間戳*/
- public $NextUnixTimePoke = 0; /* 下次快取的Unix 時間戳*/
- public $UnixNowToNext = 0; /Next = 0; / * 現在和下次快取相差的Unix 時間戳*/
public $LastTimePoke = 0; /* 上一次快取的時間*/
- public $CurrentTimePoke = 0;/* 目前快取的時間*/
- public $NextTimePoke = 0; /* 下次快取的時間*/
public $DataLength = 0; /* 快取區內容長度*/
- public $CacheToPage = ''; /* 快取檔案內容*/
- private $SplitTeam = false; /* 是否分組存放Cache檔案*/
public $Cache = false; /*是否需要緩存,用戶外界判斷*/
private $_IsCache = false; /* 是否能夠緩存*/
public function Cache($SetTimeOut = 20 ,$CacheDir = 'Cache',$SplitTeam = false,$SetExt = '.cache')
- {
- $this->CacheDir = $CacheDir;
- $this->SplitTeam = $SplitTeam;
- if(!is_numeric($SetTimeOut))
- {
- $this->ErrResponse('快取過期時間設定無效');
- return false;
- } else {
- $this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this🎜>$this ->SetTimeOut = $SetTimeOut;
- }
- $this->SetExt = $SetExt;
/* 快取開始*/
- ob_clean();
- ob_start ();
- ob_implicit_flush(0);
- $this->CreateCache();
- return true;
- }
private function CreateCache()
- {
- $_CacheFile = str_replace('.','_',basename($_SERVER['PHP_SELF'])) . '_' .
- md5(basename($_SERVER['PHP_SELF'])) . $this->SetExt;
- $_CacheConfig = str_replace('.','_',basename($_SERVER['PHP_SELF'])) . '_' . '.cof';
-
if(!file_exists($this->CacheDir))
- {
- mkdir($this->CacheDir,0777);
- }
if($ this->SplitTeam)
- {
- $_CacheConfigDir = $this->CacheDir . str_replace('.','_',basename($_SERVER['PHP_SELF'])) . '_/';
- if(!file_exists($_CacheConfigDir))
- {
- mkdir($_CacheConfigDir,0777);
- }
- $_CacheUrl = $this->CacheDir .PacheDirache. $_CacheConfigUrl = $this->CacheDir . $_CacheConfigDir . $_CacheConfig;
- } else {
- $_CacheUrl = $this->CacheDir . $_CacheFile;}
if(!file_exists($_CacheUrl))
- {
- $hanld = @fopen($_CacheUrl,"w");
- @fclose ($hanld);
- }
if(!file_exists($_CacheConfigUrl))
- {
- $hanld = @fopen($_CacheConfigUrl,"w");
- @fclose($hanld);
- }
$this->CacheConfigFile = $_CacheConfigUrl;
- $this->CacheFileUrl = $_CacheUrl;
- $this->$this- >CheckCache();
- return true;
- }
private function CheckCache()
- {
- $_FileEditTime = @filemtime($this->CacheFileUrl);
- $_TimeOut = $this->SetTimeOut;
- ;
- $_IsTimeOut = $_FileEditTime $_TimeOut;
$this->LastUnixTimePoke = $_FileEditTime;
- $this->NextUnixTimePoke = $_IsTimeOut;
- $this->CurrentUnix );
- $this->UnixNowToNext = $this->NextUnixTimePoke - time();
$this->LastTimePoke = date("Y-m-d H:i:s",$_FileEditTime );
- $this->NextTimePoke = date("Y-m-d H:i:s",$_IsTimeOut);
- $this->CurrentTimePoke = date("Y-m-d H:i:s",time()) ;
$_TxtInformation = "上次快取時間戳記: $this->LastUnixTimePoke ";
- $_TxtInformation .= "目前快取時間戳: $this->CurrentUnixTimePoke ";
- $_TxtInformation .= "下次快取時間戳: $this->NextUnixTimePoke ";
$_TxtInformation .= "上次快取時間: $this->LastTimePoke ";
- $ _TxtInformation .= "目前快取時間: $this->CurrentTimePoke ";
- $_TxtInformation .= "下次快取時間: $this->NextTimePoke ";
$_TxtInformation .= "距離下次快取戳記: $this->UnixNowToNext ";
$handl = @fopen($this->CacheConfigFile,'w');
- if($handl)
- {
- @fwrite($handl,$_TxtInformation);
- @fclose($handl);
- }
if($_IsTimeOut >= time())
- {
- $this->GetCacheData();
- }
- }
private function ClearCacheFile()
- {
- @unlink($this- >CacheFileUrl);
- @unlink($this->CacheConfigFile);
- }
public function PutCache()
- {
- $this->DataLength = ob_get_length ();
- $PutData = ob_get_contents();
- if(!file_exists($this->CacheFileUrl))
- {
- $CreateOK = $this->CreateCache();
- if (!$CreateOK)
- {
- $this->ErrResponse('檢查快取檔案時產生錯誤,快取檔案建立失敗');
- return false;
- }
- } else {
- $hanld = @fopen($this->CacheFileUrl,"w");
- if($hanld)
- {
if(@is_writable($this-> CacheFileUrl))
- {
- @flock($hanld, LOCK_EX);
- $_PutData = @fwrite($hanld,$PutData);
- @flock($hanld, LOCK_UN);
- UN);
- if(!$_PutData)
- {
- $this->ErrResponse('無法更改目前快取檔案內容');
- return false;
- } else {
- @fclose($hanld) ;
- return true;
- }
- } else {
- $this->ErrResponse('快取檔案不可寫');
- return false;
- }
- } else {
$this->ErrResponse('開啟快取檔案發生致命錯誤');
- return false;
- }
- }
- }
public function GetCacheData()
- {
- $hanld = @fopen($this->CacheFileUrl,"r");
- if($hanld)
- {
- if( @is_readable($this->CacheFileUrl))
- {
- $this->CacheToPage = @file_get_contents($this->CacheFileUrl);
- $IsEmpty = count(file($this->CacheFileUrl); ; //判斷快取檔案是否為空
if($IsEmpty > 0)
- {
- echo $this->CacheToPage;
- @fclose($hanld);
- ob_end_flush();
- exit();
- }
- } else {
- $this->ErrResponse('讀取快取檔案內容失敗');
- return false;
- }
- } else {
- $this->ErrResponse('開啟快取檔案失敗');
- return false;
- }
- }
- }
- }
private function ErrResponse($Msg)
- {