php操作memcache緩存方法分享
更新時間:2015年06月03日 12:20:46 投稿:hebedich
一般來說,如果并發(fā)量不大的情況,使不使用緩存技術(shù)并沒有什么影響,但如果高并發(fā)的情況,使用緩存技術(shù)就顯得很重要了,可以很好的減輕數(shù)據(jù)庫和服務(wù)器的壓力,當(dāng)然解決高并發(fā)的技術(shù)有很多,這里只是以緩存的角度來說明使用memcache的便捷性和方便性,
使用memcache的前提是需要在服務(wù)端先配置好memcahche的環(huán)境!確認(rè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服務(wù)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服務(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->mem->connect( $host , $port ); } catch (Exception $e) { exit('memecache連接失敗,錯誤信息:'. $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->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire); } else { return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire); } } /** * 讀取緩存 * * @access private * @param string $key 關(guān)鍵字 * @param int $sid 選擇第幾臺memcache服務(wù)器 * @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 關(guān)鍵字 * @param int $sid 選擇第幾臺memcache服務(wù)器 * @return array */ public function remove( $key , $sid) { $this->connect(); return $this->mem->delete($key); } /** * 析構(gòu)函數(shù) * 最后關(guān)閉memcache */ public function __destruct() { /*if(! $this->mem) { $this->mem->close(); }*/ } }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
相關(guān)文章
基于CakePHP實現(xiàn)的簡單博客系統(tǒng)實例
這篇文章主要介紹了基于CakePHP實現(xiàn)的簡單博客系統(tǒng),以一個完整實例分析了使用CakePHP實現(xiàn)博客系統(tǒng)的完整流程,需要的朋友可以參考下2015-06-06Zend Framework框架的校驗器InArray使用示例
這篇文章主要介紹了 zf框架的校驗器InArray使用示例框架的校驗器InArray使用示例,需要的朋友可以參考下2014-03-03ThinkPHP模板判斷輸出Defined標(biāo)簽用法詳解
這篇文章主要介紹了ThinkPHP模板判斷輸出Defined標(biāo)簽用法詳解,需要的朋友可以參考下2014-06-06使用VS?Code+phpstudy實現(xiàn)PHP環(huán)境配置指南
這篇文章主要給大家介紹了關(guān)于使用VS?Code+phpstudy實現(xiàn)PHP環(huán)境配置的相關(guān)資料,對于初學(xué)者可以使用集成開發(fā)環(huán)境PHPStudy來配置PHP環(huán)境,需要的朋友可以參考下2023-07-07利用PHP將圖片轉(zhuǎn)換成base64編碼的實現(xiàn)方法
相信大家都知道Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)代碼的編碼方式之一,如果對此不清楚的可以查看RFC2045~RFC2049,上面有MIME的詳細(xì)規(guī)范。這篇文章我們分享一個PHP將圖片轉(zhuǎn)換為base64編碼格式的方法,有需要的朋友們可以參考借鑒。2016-09-09