使用php自動(dòng)備份數(shù)據(jù)庫(kù)表的實(shí)現(xiàn)方法
772
2023-12-08
本文實(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í)行,緩存無效則運(yùn)行原頁(yè)面程序生成緩存 * ob_start() 開啟瀏覽器緩存用于在頁(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è)面代碼開始 // //頁(yè)面代碼結(jié)束 // $cache->write(); //首次運(yùn)行或緩存過期,生成緩存 ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請(qǐng)用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭(zhēng)議與本站無關(guān)。您必須在下載后的24個(gè)小時(shí)之內(nèi),從您的電腦或手機(jī)中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請(qǐng)支持正版,購(gòu)買注冊(cè),得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請(qǐng)郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請(qǐng)諒解!