您好,欢迎访问一九零五行业门户网

一个php的页面缓存类

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;}}?>
复制代码
其它类似信息

推荐信息