PHP文件緩存類
<?php class Cache { /** * $dir : 緩存文件存放目錄 * $lifetime : 緩存文件有效期,單位為秒 * $cacheid : 緩存文件路徑,包含文件名 * $ext : 緩存文件擴展名(可以不用),這里使用是為了查看文件方便 */ private $dir; private $lifetime; private $cacheid; private $ext; /** * 析構(gòu)函數(shù),檢查緩存目錄是否有效,默認賦值 */ function __construct($dir=’’,$lifetime=1800) { if ($this--->dir_isvalid($dir)) { $this->dir = $dir; $this->lifetime = $lifetime; $this->ext = ’.Php’; $this->cacheid = $this->getcacheid(); } } /** * 檢查緩存是否有效 */ private function isvalid() { if (!file_exists($this->cacheid)) return false; if (!(@$mtime = filemtime($this->cacheid))) return false; if (mktime() - $mtime > $this->lifetime) return false; return true; } /** * 寫入緩存 * $mode == 0 , 以瀏覽器緩存的方式取得頁面內(nèi)容 * $mode == 1 , 以直接賦值(通過$content參數(shù)接收)的方式取得頁面內(nèi)容 * $mode == 2 , 以本地讀取(fopen ile_get_contents)的方式取得頁面內(nèi)容(似乎這種方式?jīng)]什么必要) */ public function write($mode=0,$content=’’) { switch ($mode) { case 0: $content = ob_get_contents(); break; default: break; } ob_end_flush(); try { file_put_contents($this->cacheid,$content); } catch (Exception $e) { $this->error(’寫入緩存失敗!請檢查目錄權(quán)限!’); } } /** * 加載緩存 * exit() 載入緩存后終止原頁面程序的執(zhí)行,緩存無效則運行原頁面程序生成緩存 * ob_start() 開啟瀏覽器緩存用于在頁面結(jié)尾處取得頁面內(nèi)容 */ public function load() { if ($this->isvalid()) { echo ' '; //以下兩種方式,哪種方式好????? require_once($this->cacheid); //echo file_get_contents($this->cacheid); exit(); } else { ob_start(); } } /** * 清除緩存 */ public function clean() { try { unlink($this->cacheid); } catch (Exception $e) { $this->error(’清除緩存文件失敗!請檢查目錄權(quán)限!’); } } /** * 取得緩存文件路徑 */ private function getcacheid() { return $this->dir.md5($this->geturl()).$this->ext; } /** * 檢查目錄是否存在或是否可創(chuàng)建 */ private function dir_isvalid($dir) { if (is_dir($dir)) return true; try { mkdir($dir,0777); } catch (Exception $e) { $this->error(’所設(shè)定緩存目錄不存在并且創(chuàng)建失敗!請檢查目錄權(quán)限!’); return false; } return true; } /** * 取得當前頁面完整url */ private function geturl() { $url = ’’; if (isset($_SERVER[’REQUEST_URI’])) { $url = $_SERVER[’REQUEST_URI’]; } else { $url = $_SERVER[’Php_SELF’]; $url .= empty($_SERVER[’QUERY_STRING’])?’’:’?’.$_SERVER[’QUERY_STRING’]; } return $url; } /** * 輸出錯誤信息 */ private function error($str) { echo $str; }}?> demo.php
<php/* * 使用方法舉例 ------------------------------------Demo------------------------------------------- require_once(’cache.inc.php’); $cachedir = ’./Cache/’; //設(shè)定緩存目錄 $cache = new Cache($cachedir,10); //省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir); if ($_GET[’cacheact’] != ’rewrite’) //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作 $cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁面代碼 //頁面代碼開始 echo date(’H:i:s jS F’); //頁面代碼結(jié)束 $cache->write(); //首次運行或緩存過期,生成緩存 ------------------------------------Demo2------------------------------------------- require_once(’cache.inc.php’); $cachedir = ’./Cache/’; //設(shè)定緩存目錄 $cache = new Cache($cachedir,10); //省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir); if ($_GET[’cacheact’] != ’rewrite’) //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作 $cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁面代碼 //頁面代碼開始 $content = date(’H:i:s jS F’); echo $content; //頁面代碼結(jié)束 $cache->write(1,$content); //首次運行或緩存過期,生成緩存 ------------------------------------Demo3------------------------------------------- require_once(’cache.inc.php’); define(’CACHEENABLE’,true); if (CACHEENABLE) { $cachedir = ’./Cache/’; //設(shè)定緩存目錄 $cache = new Cache($cachedir,10); //省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir); if ($_GET[’cacheact’] != ’rewrite’) //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作 $cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁面代碼 } //頁面代碼開始 $content = date(’H:i:s jS F’); echo $content; //頁面代碼結(jié)束 if (CACHEENABLE) $cache->write(1,$content); //首次運行或緩存過期,生成緩存*/?>
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實現(xiàn)代碼4. PHP設(shè)計模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問題7. ASP.NET MVC遍歷驗證ModelState的錯誤信息8. ThinkPHP5實現(xiàn)JWT Token認證的過程(親測可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. .Net Core和RabbitMQ限制循環(huán)消費的方法
