首页 »PHP教程 » cache缓存:PHP Page Cache 页面缓存Cache类 »正文
cache缓存:PHP Page Cache 页面缓存Cache类
来源: 发布时间:星期一, 2009年1月12日 浏览:24次 评论:0
![](/icons/52876yi.gif) 个PHP Page Cache ![](/icons/52876de.gif) 页面缓存Cache类 ![](/icons/52876dou.gif) 代码结构还不错 ![](/icons/52876dou.gif) 大家有需要可以拿来用 PAGE_CACHE.PHP <?php /* $cache = Cache("../cache/",20); // 构造![](/icons/52876hanshu.gif) 创建缓存Cache类对象 ……………………………… ……………………………… ……………………………… $cache->PutCache ; // 倒出缓存Cache */
Cache { private $CacheDir = ’Cache’; /* 缓存Cache目录 */ private $SetTimeOut = 10; /* 缓存Cache过期时间 */ private $SetExt = ’.cache’; /* 缓存Cache文件后缀名 */ private $CacheFileUrl = ’’; /* 缓存Cache文件所在地址 */ private $CacheConfigFile = ’’; /* 缓存Cache文件配置信息 */ public $LastUnixTimePoke = 0; /* 上 次缓存Cache Unix 时间戳 */ public $CurrentUnixTimePoke = 0;/* 当前缓存Cache Unix 时间戳 */ public $NextUnixTimePoke = 0; /* 下 次缓存Cache Unix 时间戳 */ public $UnixNowToNext = 0; /* 现在和下 次缓存Cache相差 Unix 时间戳 */ public $LastTimePoke = 0; /* 上 次缓存Cache 时间 */ public $CurrentTimePoke = 0;/* 当前缓存Cache 时间 */ public $NextTimePoke = 0; /* 下 次缓存Cache 时间 */ public $DataLength = 0; /* 缓存Cache区内容长度 */ public $CacheToPage = ’’; /* 缓存Cache文件内容 */ private $SplitTeam = false; /* 是否分组存放Cache文件 */ public $Cache = false; /* 是否需要缓存Cache 用户外界判断 */ private $_IsCache = false; /* 是否能够缓存Cache */ public function Cache( $SetTimeOut = 20, $CacheDir = ’Cache’, $SplitTeam = false, $SetExt = ’.cache’) { $this->CacheDir = $CacheDir; $this->SplitTeam = $SplitTeam; (!is_numeric( $SetTimeOut)) { $this->ErrResponse(’缓存Cache过期时间设置无效’); false; } { $this->SetTimeOut = $SetTimeOut; } $this->SetExt = $SetExt; /* 缓存Cache开始 */ ob_clean ; ob_start ; ob_implicit_flush(0); $this->CreateCache ; true; } private function CreateCache![](/icons/52876kh.gif) { $_CacheFile = str_replace(’.’,’_’,basename( $_SERVER[’PHP_SELF’])) . ’_’ . md5(basename( $_SERVER[’PHP_SELF’])) . $this->SetExt; $_CacheConfig = str_replace(’.’,’_’,basename( $_SERVER[’PHP_SELF’])) . ’_’ . ’.cof’; (!file_exists( $this->CacheDir)) { mkdir( $this->CacheDir,0777); } ( $this->SplitTeam) { $_CacheConfigDir = $this->CacheDir . str_replace(’.’,’_’,basename( $_SERVER[’PHP_SELF’])) . ’_/’; (!file_exists( $_CacheConfigDir)) { mkdir( $_CacheConfigDir,0777); } $_CacheUrl = $this->CacheDir . $_CacheConfigDir . $_CacheFile; $_CacheConfigUrl = $this->CacheDir . $_CacheConfigDir . $_CacheConfig; } { $_CacheUrl = $this->CacheDir . $_CacheFile; $_CacheConfigUrl = $this->CacheDir . $_CacheConfig; } (!file_exists( $_CacheUrl)) { $hanld = @fopen( $_CacheUrl,"w"); @fclose( $hanld); } (!file_exists( $_CacheConfigUrl)) { $hanld = @fopen( $_CacheConfigUrl,"w"); @fclose( $hanld); } $this->CacheConfigFile = $_CacheConfigUrl; $this->CacheFileUrl = $_CacheUrl; $this->CheckCache ; true; } private function CheckCache![](/icons/52876kh.gif) { $_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 = "上次缓存Cache时间戳: $this->LastUnixTimePoke "; $_TxtInformation .= "当前缓存Cache时间戳: $this->CurrentUnixTimePoke "; $_TxtInformation .= "下次缓存Cache时间戳: $this->NextUnixTimePoke "; $_TxtInformation .= "上次缓存Cache时间: $this->LastTimePoke "; $_TxtInformation .= "当前缓存Cache时间: $this->CurrentTimePoke "; $_TxtInformation .= "下次缓存Cache时间: $this->NextTimePoke "; $_TxtInformation .= "距离下次缓存Cache戳: $this->UnixNowToNext "; $handl = @fopen( $this->CacheConfigFile,’w’); ( $handl) { @fwrite( $handl, $_TxtInformation); @fclose( $handl); } ( $_IsTimeOut >= time ) { $this->GetCacheData ; } } private function ClearCacheFile![](/icons/52876kh.gif) { @unlink( $this->CacheFileUrl); @unlink( $this->CacheConfigFile); } public function PutCache![](/icons/52876kh.gif) { $this->DataLength = ob_get_length ; $PutData = ob_get_contents ; (!file_exists( $this->CacheFileUrl)) { $CreateOK = $this->CreateCache ; (! $CreateOK) { $this->ErrResponse(’检查缓存Cache文件时产生![](/icons/52876cuowu.gif) 缓存Cache文件创建失败’); false; } } { $hanld = @fopen( $this->CacheFileUrl,"w"); ( $hanld) { (@is_writable( $this->CacheFileUrl)) { @flock( $hanld, LOCK_EX); $_PutData = @fwrite( $hanld, $PutData); @flock( $hanld, LOCK_UN); (! $_PutData) { $this->ErrResponse(’无法更改当前缓存Cache文件内容’); false; } { @fclose( $hanld); true; } } { $this->ErrResponse(’缓存Cache文件不可写’); false; } } { $this->ErrResponse(’打开缓存Cache文件发生致命 ’); false; } } } public function GetCacheData![](/icons/52876kh.gif) { $hanld = @fopen( $this->CacheFileUrl,"r"); ( $hanld) { (@is_readable( $this->CacheFileUrl)) { $this->CacheToPage = @file_get_contents( $this->CacheFileUrl); $IsEmpty = count(file( $this->CacheFileUrl)); //判断缓存Cache文件是否为空 ( $IsEmpty > 0) { echo $this->CacheToPage; @fclose( $hanld); ob_end_flush ; exit ; } } { $this->ErrResponse(’读取缓存Cache文件内容失败’); false; } } { $this->ErrResponse(’打开缓存Cache文件失败’); false; } } private function ErrResponse( $Msg) { echo $Msg; } } ?>
相关文章
读者评论
发表评论
|
|