這篇文章主要介紹了PHP實現的簡單緩存類,實例分析了php緩存文件的定義及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了PHP實現的簡單緩存類。分享給大家供大家參考。具體如下:
cache.inc.php:
- <?php
- class Cache {
- /**
- * $dir : 緩存文件存放目錄
- * $lifetime : 緩存文件有效期,單位為秒
- * $cacheid : 緩存文件路徑,包含文件名
- * $ext : 緩存文件擴展名(可以不用),這里使用是為了查看文件方便
- */
- private $dir;
- private $lifetime;
- private $cacheid;
- private $ext;
- /**
- * 析構函數,檢查緩存目錄是否有效,默認賦值
- */
- 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 , 以瀏覽器緩存的方式取得頁面內容
- * $mode == 1 , 以直接賦值(通過$content參數接收)的方式取得頁面內容
- * $mode == 2 , 以本地讀取(fopen ile_get_contents)的方式取得頁面內容(似乎這種方式沒什么必要)
- */
- 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('寫入緩存失敗!請檢查目錄權限!');
- }
- }
- /**
- * 加載緩存
- * exit() 載入緩存后終止原頁面程序的執行,緩存無效則運行原頁面程序生成緩存
- * ob_start() 開啟瀏覽器緩存用于在頁面結尾處取得頁面內容
- */
- public function load() {
- if ($this->isvalid()) {
- echo "<span style='display:none;'>This is Cache.</span> ";
- //以下兩種方式,哪種方式好?????
- 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('清除緩存文件失敗!請檢查目錄權限!');
- }
- }
- /**
- * 取得緩存文件路徑
- */
- private function getcacheid() {
- return $this->dir.md5($this->geturl()).$this->ext;
- }
- /**
- * 檢查目錄是否存在或是否可創建
- */
- private function dir_isvalid($dir) {
- if (is_dir($dir)) return true;
- try {
- mkdir($dir,0777);
- }
- catch (Exception $e) {
- $this->error('所設定緩存目錄不存在并且創建失敗!請檢查目錄權限!');
- 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 '<div style="color:red;">'.$str.'</div>';
- }
- }
- ?>
demo.php:
- <?php
- /*
- * 可自由轉載使用,請保留版權信息,謝謝使用!
- * Class Name : Cache (For Php5)
- * Version : 1.0
- * Description : 動態緩存類,用于控制頁面自動生成緩存、調用緩存、更新緩存、刪除緩存.
- * Last Modify : 2007-8-22
- * Remark :
- 1.此版本為Php5版本,本人暫沒有寫Php4的版本,如需要請自行參考修改(比較容易啦,不要那么懶嘛,呵呵!).
- 2.此版本為utf-8編碼,如果網站采用其它編碼請自行轉換,Windows系統用記事本打開另存為,選擇相應編碼即可(一般ANSI),Linux下請使用相應編輯軟件或iconv命令行.
- 3.拷貝粘貼的就不用管上面第2條了.
- * 關于緩存的一點感想:
- * 動態緩存和靜態緩存的根本差別在于其是自動的,用戶訪問頁面過程就是生成緩存、瀏覽緩存、更新緩存的過程,無需人工操作干預.
- * 靜態緩存指的就是生成靜態頁面,相關操作一般是在網站后臺完成,需人工操作(也就是手動生成).
- */
- /*
- * 使用方法舉例*/
- //Demo1:
- require_once('cache.inc.php');
- $cachedir = './Cache/'; //設定緩存目錄
- $cache = new Cache($cachedir,10); //省略參數即采用缺省設置, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設定一些其它操作
- $cache->load(); //裝載緩存,緩存有效則不執行以下頁面代碼
- //頁面代碼開始
- echo date('H:i:s jS F');
- //頁面代碼結束
- $cache->write(); //首次運行或緩存過期,生成緩存
- //Demo2:
- require_once('cache.inc.php');
- $cachedir = './Cache/'; //設定緩存目錄
- $cache = new Cache($cachedir,10); //省略參數即采用缺省設置, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設定一些其它操作
- $cache->load(); //裝載緩存,緩存有效則不執行以下頁面代碼
- //頁面代碼開始
- $content = date('H:i:s jS F');
- echo $content;
- //頁面代碼結束
- $cache->write(1,$content); //首次運行或緩存過期,生成緩存
- //Demo3:
- require_once('cache.inc.php');
- define('CACHEENABLE',true);
- if (CACHEENABLE) {
- $cachedir = './Cache/'; //設定緩存目錄
- $cache = new Cache($cachedir,10); //省略參數即采用缺省設置, $cache = new Cache($cachedir);
- if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設定一些其它操作
- $cache->load(); //裝載緩存,緩存有效則不執行以下頁面代碼
- }
- //頁面代碼開始
- $content = date('H:i:s jS F');
- echo $content;
- //頁面代碼結束
- if (CACHEENABLE)
- $cache->write(1,$content); //首次運行或緩存過期,生成緩存
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答