-
-
/* $cache = new Cache("../cache/",20); // Constructor, creates cache class object - $cache->PutCache (); // Pour out cache
- */
- class Cache
- {
- private $CacheDir = 'Cache'; /* Cache directory*/
- private $SetTimeOut = 10; /* Cache expiration time*/
- private $SetExt = '.cache'; /* Cache file suffix name*/
- private $CacheFileUrl = ''; /* Address of cache file*/
- private $CacheConfigFile = ''; /* Cache file configuration information*/
public $LastUnixTimePoke = 0; /* Last cached Unix timestamp*/
- public $CurrentUnixTimePoke = 0;/* Current cached Unix timestamp*/
- public $NextUnixTimePoke = 0; /* Next time Cached Unix timestamp*/
- public $UnixNowToNext = 0; /* Unix timestamp difference between now and next cache*/
public $LastTimePoke = 0; /* Last cached Time*/
- public $CurrentTimePoke = 0;/* Current cached time*/
- public $NextTimePoke = 0; /* Next cached time*/
public $DataLength = 0; /* Cache area content length*/
- public $CacheToPage = ''; /* Cache file content*/
- private $SplitTeam = false; /* Whether to store Cache files in groups*/
public $Cache = false; /* Whether caching is needed, the user can judge externally*/
private $_IsCache = false; /* Whether caching is possible*/
public function Cache($SetTimeOut = 20,$CacheDir = 'Cache',$SplitTeam = false,$SetExt = '.cache')
- {
- $this->CacheDir = $CacheDir;
- $this->SplitTeam = $ SplitTeam;
- if(!is_numeric($SetTimeOut))
- {
- $this->ErrResponse('The cache expiration time setting is invalid');
- return false;
- } else {
- $this->SetTimeOut = $SetTimeOut;
- }
- $this->SetExt = $SetExt;
/* Cache start*/
- 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 . $_CacheConfig;
- }
if(!file_exists($_CacheUrl))
- {
- $hanld = @fopen($_CacheUrl,"w");
- @fclose($hanld);
- }
if(!file_exists($_CacheConfigUrl))
- {
- $hanld = @fopen($_CacheConfigUrl,"w");
- @fclose($hanld);
- } p>
$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());
- < ;p>$_TxtInformation = "Last cache timestamp: $this->LastUnixTimePoke ";
- $_TxtInformation .= "Current cache timestamp: $this->CurrentUnixTimePoke ";
- $_TxtInformation .= "Next cache time Poke: $this->NextUnixTimePoke ";
$_TxtInformation .= "Last cache time: $this->LastTimePoke ";
- $_TxtInformation .= "Current cache time: $this ->CurrentTimePoke ";
- $_TxtInformation .= "Next cache time: $this->NextTimePoke ";
$_TxtInformation .= "Next cache stamp: $this-> ;UnixNowToNext ";
$handl = @fopen($this->CacheConfigFile,'w');
- if($handl)
- {
- @fwrite($handl,$_TxtInformation) ;
- @fclose($handl);
- }
if($_IsTimeOut >= time())
- {
- $this->GetCacheData();
- }
- }< /p>
private function ClearCacheFile()
- {
- @unlink($this->CacheFileUrl);
- @unlink($this->CacheConfigFile);
- }
{ - $this->DataLength = ob_get_length();
- $PutData = ob_get_contents();
- if(!file_exists($this->CacheFileUrl))
- {
- $CreateOK = $this ->CreateCache();
- if(!$CreateOK)
- {
- $this->ErrResponse('An error occurred while checking the cache file and the cache file creation failed');
- 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('Cannot change the content of the current cached file');
- return false;
- } else {
- @fclose($hanld);
- return true;
- }
- } else {
- $this->ErrResponse('The cached file cannot be write');
- return false;
- }
- } else {
$this->ErrResponse('A fatal error occurred while opening the cache file');
- 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)); //Judge Whether the cache file is empty
if($IsEmpty > 0)
- {
- echo $this->CacheToPage;
- @fclose($hanld);
- ob_end_flush();
- exit( );
- }
- } else {
- $this->ErrResponse('Failed to read cache file content');
- return false;
- }
- } else {
- $this->ErrResponse('Failed to open cache file' );
- return false;
- }
- }
private function ErrResponse($Msg)
- {
- echo $Msg;
- }
- }
- ?>
-
Copy code
|