本篇文章給大家分享的是有關利用php怎么對redis緩存進行操作,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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服務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服務 * * @access private * @param string $key 關鍵字 * @param string $value 緩存內容 * @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服務選擇 if ( ! $server[$sid]) { throw new QException('當前操作的緩存服務器配置文件不存在'); } $host = $server[$sid]['host']; $port = $server[$sid]['port']; try { $this->redis->connect( $host , $port ); } catch (Exception $e) { exit('memecache連接失敗,錯誤信息:'. $e->getMessage()); } } /** * 寫入緩存 * * @access private * @param string $key 關鍵字 * @param string $value 緩存內容 * @return array */ public function set( $key , $value , $sid , $expire = 0) { $data = $this->get($key , $sid); //如果已經存在key值 if( $data ) { return $this->redis->getset( $key , $value); } else { return $this->redis->set( $key , $value); } } /** * 讀取緩存 * * @access private * @param string $key 關鍵字 * @param int $sid 選擇第幾臺memcache服務器 * @return array */ public function get( $key , $sid) { $this->connect( $sid ); return $this->redis->get($key); } /** * 清洗(刪除)已經存儲的所有的元素 * * @access private * @return array */ public function flush() { $this->connect(); return $this->redis->flushall(); } /** * 刪除緩存 * * @access private * @param string $key 關鍵字 * @param int $sid 選擇第幾臺memcache服務器 * @return array */ public function remove( $key , $sid) { $this->connect(); return $this->redis->del($key); } /** * 析構函數 * 最后關閉memcache */ public function __destruct() { if($this->redis) { $this->redis->close(); } } }
以上就是利用php怎么對redis緩存進行操作,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。