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

