CI框架常用函數(shù)封裝實例
695
2024-01-20
使用memcache的前提是需要在服務端先配置好memcahche的環(huán)境!確認memcahce可以正常連接之后就可以在程序使用了!
<?php /** * Memcache緩存操作 * @author hxm * @version 1.0 * @since 2015.05.04 */ class MCache extends Object implements CacheFace { private $mem = null; //Mem對象 private $sId = 1; //servier服務ID /** * 初始化Memcache * * @return Object */ public function __construct() { if ( !class_exists('Memcache') ) { throw new QException('PHP extension does not exist: Memcache'); } $this->mem = new Memcache(); } /** * 鏈接memcahce服務 * * @access private * @param string $key 關鍵字 * @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服務選擇 if ( ! $server[$sid]) { throw new QException('當前操作的緩存服務器配置文件不存在'); } $host = $server[$sid]['host']; $port = $server[$sid]['port']; try { $this->mem->connect( $host , $port ); } catch (Exception $e) { exit('memecache連接失敗,錯誤信息:'. $e->getMessage()); } } /** * 寫入緩存 * * @access private * @param string $key 關鍵字 * @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->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire); } else { return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire); } } /** * 讀取緩存 * * @access private * @param string $key 關鍵字 * @param int $sid 選擇第幾臺memcache服務器 * @return array */ public function get( $key , $sid) { $this->connect( $sid ); return $this->mem->get($key); } /** * 清洗(刪除)已經(jīng)存儲的所有的元素 * * @access private * @return array */ public function flush() { $this->connect(); return $this->mem->flush(); } /** * 刪除緩存 * * @access private * @param string $key 關鍵字 * @param int $sid 選擇第幾臺memcache服務器 * @return array */ public function remove( $key , $sid) { $this->connect(); return $this->mem->delete($key); } /** * 析構函數(shù) * 最后關閉memcache */ public function __destruct() { /*if(! $this->mem) { $this->mem->close(); }*/ } }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內(nèi)容信息僅限用于學習和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請用戶自負。本站信息來自網(wǎng)絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內(nèi),從您的電腦或手機中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯(lián)系進行刪除處理。敬請諒解!