php操作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對(duì)象 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連接失敗,錯(cuò)誤信息:'. $e->getMessage()); } } /** * 寫(xiě)入緩存 * * @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 選擇第幾臺(tái)memcache服務(wù)器 * @return array */ public function get( $key , $sid) { $this->connect( $sid ); return $this->mem->get($key); } /** * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素 * * @access private * @return array */ public function flush() { $this->connect(); return $this->mem->flush(); } /** * 刪除緩存 * * @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->mem->delete($key); } /** * 析構(gòu)函數(shù) * 最后關(guān)閉memcache */ public function __destruct() { /*if(! $this->mem) { $this->mem->close(); }*/ } }
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- PHP MemCached高級(jí)緩存配置圖文教程
- Memcache 基礎(chǔ)教程(php 緩存)
- php實(shí)現(xiàn)memcache緩存示例講解
- PHP 內(nèi)存緩存加速功能memcached安裝與用法
- PHP MemCached 高級(jí)緩存應(yīng)用代碼
- php中操作memcached緩存進(jìn)行增刪改查數(shù)據(jù)的實(shí)現(xiàn)代碼
- PHP內(nèi)存緩存Memcached類(lèi)實(shí)例
- PHP使用memcache緩存技術(shù)提高響應(yīng)速度的方法
- PHP內(nèi)存緩存功能memcached示例
- 利用php操作memcache緩存的基礎(chǔ)方法示例
相關(guān)文章
基于CakePHP實(shí)現(xiàn)的簡(jiǎn)單博客系統(tǒng)實(shí)例
這篇文章主要介紹了基于CakePHP實(shí)現(xiàn)的簡(jiǎn)單博客系統(tǒng),以一個(gè)完整實(shí)例分析了使用CakePHP實(shí)現(xiàn)博客系統(tǒng)的完整流程,需要的朋友可以參考下2015-06-06Zend Framework框架的校驗(yàn)器InArray使用示例
這篇文章主要介紹了 zf框架的校驗(yàn)器InArray使用示例框架的校驗(yàn)器InArray使用示例,需要的朋友可以參考下2014-03-03ThinkPHP模板判斷輸出Defined標(biāo)簽用法詳解
這篇文章主要介紹了ThinkPHP模板判斷輸出Defined標(biāo)簽用法詳解,需要的朋友可以參考下2014-06-06HTTP頭隱藏PHP版本號(hào)實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了HTTP頭隱藏PHP版本號(hào)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12使用VS?Code+phpstudy實(shí)現(xiàn)PHP環(huán)境配置指南
這篇文章主要給大家介紹了關(guān)于使用VS?Code+phpstudy實(shí)現(xiàn)PHP環(huán)境配置的相關(guān)資料,對(duì)于初學(xué)者可以使用集成開(kāi)發(fā)環(huán)境PHPStudy來(lái)配置PHP環(huán)境,需要的朋友可以參考下2023-07-07利用PHP將圖片轉(zhuǎn)換成base64編碼的實(shí)現(xiàn)方法
相信大家都知道Base64是網(wǎng)絡(luò)上最常見(jiàn)的用于傳輸8Bit字節(jié)代碼的編碼方式之一,如果對(duì)此不清楚的可以查看RFC2045~RFC2049,上面有MIME的詳細(xì)規(guī)范。這篇文章我們分享一個(gè)PHP將圖片轉(zhuǎn)換為base64編碼格式的方法,有需要的朋友們可以參考借鑒。2016-09-09