/* $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; /* 現在和下一次緩衝相差的 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->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 . $_CacheConfigDir . $_CacheFile;
- $_CacheConfigUrl = $this->CacheDir . $_CacheConfigDir . $_CacheConfig;
- } else {
- $_CacheUrl = $this->CacheDir . $_CacheFile;
- $_CacheConfigUrl = $this->CacheDir . $_CacheConfig;
- }
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->CheckCache();
- return true;
- }
private function CheckCache()
- {
- $_FileEditTime = @filemtime($this->CacheFileUrl);
- $_TimeOut = $this->SetTimeOut;
- $_IsTimeOut = $_FileEditTime $_TimeOut;
$this->LastUnixTimePoke = $_FileEditTime;
- $this->NextUnixTimePoke = $_IsTimeOut;
- $this->CurrentUnixTimePoke = time();
- $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);
- 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)
- {
- echo $Msg;
- }
- }
- ?>