国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

PHP實(shí)現(xiàn)的簡(jiǎn)單緩存類

2024-04-25 20:40:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了PHP實(shí)現(xiàn)的簡(jiǎn)單緩存類。分享給大家供大家參考。具體如下:

cache.inc.php:

<?php

class Cache {

/**

* $dir : 緩存文件存放目錄

* $lifetime : 緩存文件有效期,單位為秒

* $cacheid : 緩存文件路徑,包含文件名

* $ext : 緩存文件擴(kuò)展名(可以不用),這里使用是為了查看文件方便

*/

private $dir;

private $lifetime;

private $cacheid;

private $ext;

/**

* 析構(gòu)函數(shù),檢查緩存目錄是否有效,默認(rèn)賦值

*/

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;

}

/**

* 寫(xiě)入緩存

* $mode == 0 , 以瀏覽器緩存的方式取得頁(yè)面內(nèi)容

* $mode == 1 , 以直接賦值(通過(guò)$content參數(shù)接收)的方式取得頁(yè)面內(nèi)容

* $mode == 2 , 以本地讀取(fopen ile_get_contents)的方式取得頁(yè)面內(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('寫(xiě)入緩存失敗!請(qǐng)檢查目錄權(quán)限!');

}

}

/**

* 加載緩存

* exit() 載入緩存后終止原頁(yè)面程序的執(zhí)行,緩存無(wú)效則運(yùn)行原頁(yè)面程序生成緩存

* ob_start() 開(kāi)啟瀏覽器緩存用于在頁(yè)面結(jié)尾處取得頁(yè)面內(nèi)容

*/

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('清除緩存文件失敗!請(qǐng)檢查目錄權(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)建失敗!請(qǐng)檢查目錄權(quán)限!');

return false;

}

return true;

}

/**

* 取得當(dāng)前頁(yè)面完整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;

}

/**

* 輸出錯(cuò)誤信息

*/

private function error($str) {

echo '<div style="color:red;">'.$str.'</div>';

}

}

?>

demo.php:

<?php

/*

* 可自由轉(zhuǎn)載使用,請(qǐng)保留版權(quán)信息,謝謝使用!

* Class Name : Cache (For Php5)

* Version : 1.0

* Description : 動(dòng)態(tài)緩存類,用于控制頁(yè)面自動(dòng)生成緩存、調(diào)用緩存、更新緩存、刪除緩存.

* Last Modify : 2007-8-22

* Remark :

1.此版本為Php5版本,本人暫沒(méi)有寫(xiě)Php4的版本,如需要請(qǐng)自行參考修改(比較容易啦,不要那么懶嘛,呵呵!).

2.此版本為utf-8編碼,如果網(wǎng)站采用其它編碼請(qǐng)自行轉(zhuǎn)換,Windows系統(tǒng)用記事本打開(kāi)另存為,選擇相應(yīng)編碼即可(一般ANSI),Linux下請(qǐng)使用相應(yīng)編輯軟件或iconv命令行.

3.拷貝粘貼的就不用管上面第2條了.

* 關(guān)于緩存的一點(diǎn)感想:

* 動(dòng)態(tài)緩存和靜態(tài)緩存的根本差別在于其是自動(dòng)的,用戶訪問(wèn)頁(yè)面過(guò)程就是生成緩存、瀏覽緩存、更新緩存的過(guò)程,無(wú)需人工操作干預(yù).

* 靜態(tài)緩存指的就是生成靜態(tài)頁(yè)面,相關(guān)操作一般是在網(wǎng)站后臺(tái)完成,需人工操作(也就是手動(dòng)生成).

*/

/*

* 使用方法舉例*/

//Demo1:

require_once('cache.inc.php');

$cachedir = './Cache/'; //設(shè)定緩存目錄

$cache = new Cache($cachedir,10); //省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir);

if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過(guò)xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作

$cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁(yè)面代碼

//頁(yè)面代碼開(kāi)始

echo date('H:i:s jS F');

//頁(yè)面代碼結(jié)束

$cache->write(); //首次運(yùn)行或緩存過(guò)期,生成緩存

//Demo2:

require_once('cache.inc.php');

$cachedir = './Cache/'; //設(shè)定緩存目錄

$cache = new Cache($cachedir,10); //省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir);

if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過(guò)xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作

$cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁(yè)面代碼

//頁(yè)面代碼開(kāi)始

$content = date('H:i:s jS F');

echo $content;

//頁(yè)面代碼結(jié)束

$cache->write(1,$content); //首次運(yùn)行或緩存過(guò)期,生成緩存

//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') //此處為一技巧,通過(guò)xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作

$cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁(yè)面代碼

}

//頁(yè)面代碼開(kāi)始

$content = date('H:i:s jS F');

echo $content;

//頁(yè)面代碼結(jié)束

if (CACHEENABLE)

$cache->write(1,$content); //首次運(yùn)行或緩存過(guò)期,生成緩存

?>

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀柔区| 民勤县| 庄浪县| 宜春市| 当雄县| 银川市| 嘉鱼县| 宜兰市| 观塘区| 土默特左旗| 沿河| 丰镇市| 泸西县| 平安县| 老河口市| 嘉鱼县| 麦盖提县| 墨玉县| 青冈县| 延边| 沭阳县| 满洲里市| 沿河| 寻甸| 绥中县| 大丰市| 濮阳县| 澄江县| 正宁县| 中西区| 香格里拉县| 浮梁县| 双辽市| 比如县| 日照市| 孝义市| 信宜市| 湘乡市| 石渠县| 河曲县| 龙里县|