php實現(xiàn)的redis緩存類定義與使用方法示例
本文實例講述了php實現(xiàn)的redis緩存類定義與使用方法。分享給大家供大家參考,具體如下:
php+redis緩存類
<?php
class redisCache {
/**
* $host : redis服務器ip
* $port : redis服務器端口
* $lifetime : 緩存文件有效期,單位為秒
* $cacheid : 緩存文件路徑,包含文件名
*/
private $host;
private $port;
private $lifetime;
private $cacheid;
private $data;
public $redis;
/**
* 析構(gòu)函數(shù),檢查緩存目錄是否有效,默認賦值
*/
function __construct($lifetime=1800) {
//配置
$this->host = "127.0.0.1";
$this->port = "6379";
$redis = new Redis();
$redis->pconnect($this->host,$this->port);
$this->redis=$redis;
$this->cacheid = $this->getcacheid();
$this->lifetime = $lifetime;
$this->data=$redis->hMGet($this->cacheid, array('content','creattime'));
//print_r($this->redis);
//print_r($this->data);
}
/**
* 檢查緩存是否有效
*/
private function isvalid(){
$data=$this->data;
if (!$data['content']) return false;
if (time() - $data['creattime'] > $this->lifetime) return false;
return true;
}
/**
* 寫入緩存
* $mode == 0 , 以瀏覽器緩存的方式取得頁面內(nèi)容
*/
public function write($mode=0,$content='') {
switch ($mode) {
case 0:
$content = ob_get_contents();
break;
default:
break;
}
ob_end_flush();
try {
$this->redis->hMset($this->cacheid, array('content'=>$content,'creattime'=>time()));
$this->redis->expireAt($this->cacheid, time() + $this->lifetime);
}
catch (Exception $e) {
$this->error('寫入緩存失敗!');
}
}
/**
* 加載緩存
* exit() 載入緩存后終止原頁面程序的執(zhí)行,緩存無效則運行原頁面程序生成緩存
* ob_start() 開啟瀏覽器緩存用于在頁面結(jié)尾處取得頁面內(nèi)容
*/
public function load() {
if ($this->isvalid()) {
echo $this->data['content'];
exit();
}
else {
ob_start();
}
}
/**
* 清除緩存
*/
public function clean() {
try {
$this->redis->hDel($this->cacheid, array('content','creattime'));
}
catch (Exception $e) {
$this->error('清除緩存失敗!');
}
}
/**
* 取得緩存文件路徑
*/
private function getcacheid() {
return $this->dir.md5($this->geturl()).$this->ext;
}
/**
* 取得當前頁面完整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>';
}
}
//用法:
// require_once('redisCache.php');
// $cache = new redisCache(10); //設(shè)置緩存生存期
// if ($_GET['clearCache']) $cache->clean();
// else $cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁面代碼
// //頁面代碼開始
// //頁面代碼結(jié)束
// $cache->write(); //首次運行或緩存過期,生成緩存
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php Redis函數(shù)用法實例總結(jié)【附php連接redis單例類】
- PHP實現(xiàn)的Redis多庫選擇功能單例類
- php操作redis中的hash和zset類型數(shù)據(jù)的方法和代碼例子
- PHP實現(xiàn)操作redis的封裝類完整實例
- php 使用redis鎖限制并發(fā)訪問類示例
- PHP實現(xiàn)的Redis操作通用類示例
- PHP操作redis實現(xiàn)的分頁列表,新增,刪除功能封裝類與用法示例
- PHP基于redis計數(shù)器類定義與用法示例
- PHP購物車類Cart.class.php定義與用法示例
- php實現(xiàn)仿寫CodeIgniter的購物車類
- PHP+redis實現(xiàn)的購物車單例類示例
相關(guān)文章
php+jQuery ajax實現(xiàn)的實時刷新顯示數(shù)據(jù)功能示例
這篇文章主要介紹了php+jQuery ajax實現(xiàn)的實時刷新顯示數(shù)據(jù)功能,結(jié)合實例形式分析了php結(jié)合jQuery ajax實時刷新讀取顯示數(shù)據(jù)庫數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
PhpMyAdmin出現(xiàn)export.php Missing parameter: what /export_type錯
PhpMyAdmin出現(xiàn)export.php: Missing parameter: what /export_type錯誤,有碰到同樣問題的朋友可參考一下2012-08-08
PHP取整函數(shù):ceil,floor,round,intval的區(qū)別詳細解析
以下是對PHP中的取整函數(shù):ceil,floor,round,intval的區(qū)別進行了詳細的介紹,需要的朋友可以過來參考下2013-08-08
mysql_escape_string()函數(shù)用法分析
這篇文章主要介紹了mysql_escape_string()函數(shù)用法,結(jié)合實例形式講述了mysql_escape_string()函數(shù)的功能,并分析了mysql_escape_string的使用技巧與注意事項,需要的朋友可以參考下2016-04-04
php is_file()和is_dir()用于遍歷目錄時用法注意事項
遍歷一個目錄并區(qū)分顯示其中的文件和子目錄文件夾的實現(xiàn)代碼。2010-03-03
Zend Studio去除編輯器的語法警告設(shè)置方法
Zend Studio是PHP開發(fā)者的首選開發(fā)工具,其地位相當于微軟開發(fā)工具中的Visual Studio。Zend Studio的編輯器可以幫我們指出語法錯誤和警告,但是太多的警告有時讓我們的代碼看起來很亂,很不舒服2012-10-10
使用PHP接收POST數(shù)據(jù),解析json數(shù)據(jù)
本篇文章是對使用PHP接收POST數(shù)據(jù)以及json數(shù)據(jù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06

