php redis緩存操作
<?php /** * Redis緩存操作 * @author hxm * @version 1.0 * @since 2015.05.04 */ class RCache extends Object implements CacheFace { private $redis = null; //redis對象 private $sId = 1; //servier服務(wù)ID private $con = null;//鏈接資源 /** * 初始化Redis * * @return Object */ public function __construct() { if ( !class_exists('Redis') ) { throw new QException('PHP extension does not exist: Redis'); } $this->redis = new Redis(); } /** * 鏈接memcahce服務(wù) * * @access private * @param string $key 關(guān)鍵字 * @param string $value 緩存內(nèi)容 * @return array */ private function connect( $sid ) { $file = $this->CacheFile(); require $file; if(! isset($cache) ) { throw new QException('緩存配置文件不存在'.$file); } $server = $cache[$this->cacheId]; $sid = isset($sid) == 0 ? $this->sId : $sid;//memcache服務(wù)選擇 if ( ! $server[$sid]) { throw new QException('當(dāng)前操作的緩存服務(wù)器配置文件不存在'); } $host = $server[$sid]['host']; $port = $server[$sid]['port']; try { $this->redis->connect( $host , $port ); } catch (Exception $e) { exit('memecache連接失敗,錯(cuò)誤信息:'. $e->getMessage()); } } /** * 寫入緩存 * * @access private * @param string $key 關(guān)鍵字 * @param string $value 緩存內(nèi)容 * @return array */ public function set( $key , $value , $sid , $expire = 0) { $data = $this->get($key , $sid); //如果已經(jīng)存在key值 if( $data ) { return $this->redis->getset( $key , $value); } else { return $this->redis->set( $key , $value); } } /** * 讀取緩存 * * @access private * @param string $key 關(guān)鍵字 * @param int $sid 選擇第幾臺(tái)memcache服務(wù)器 * @return array */ public function get( $key , $sid) { $this->connect( $sid ); return $this->redis->get($key); } /** * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素 * * @access private * @return array */ public function flush() { $this->connect(); return $this->redis->flushall(); } /** * 刪除緩存 * * @access private * @param string $key 關(guān)鍵字 * @param int $sid 選擇第幾臺(tái)memcache服務(wù)器 * @return array */ public function remove( $key , $sid) { $this->connect(); return $this->redis->del($key); } /** * 析構(gòu)函數(shù) * 最后關(guān)閉memcache */ public function __destruct() { if($this->redis) { $this->redis->close(); } } }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
PHP怎么學(xué)習(xí)?PHP怎么入門?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)