php操作memcache緩存方法分享
更新時(shí)間:2015年06月03日 12:20:46 投稿:hebedich
一般來(lái)說,如果并發(fā)量不大的情況,使不使用緩存技術(shù)并沒有什么影響,但如果高并發(fā)的情況,使用緩存技術(shù)就顯得很重要了,可以很好的減輕數(shù)據(jù)庫(kù)和服務(wù)器的壓力,當(dāng)然解決高并發(fā)的技術(shù)有很多,這里只是以緩存的角度來(lái)說明使用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());
}
}
/**
* 寫入緩存
*
* @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類實(shí)例
- PHP使用memcache緩存技術(shù)提高響應(yīng)速度的方法
- PHP內(nèi)存緩存功能memcached示例
- 利用php操作memcache緩存的基礎(chǔ)方法示例
相關(guān)文章
PHP筆記之:基于面向?qū)ο笤O(shè)計(jì)的詳解
本篇文章對(duì)面向?qū)ο笤O(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
詳解PHP文件的自動(dòng)加載(autoloading)
這篇文章主要介紹了詳解PHP文件的自動(dòng)加載(autoloading)相關(guān)知識(shí)點(diǎn)以及詳細(xì)用法,有這方面需要的朋友參考下吧。2018-02-02
thinkphp5框架調(diào)用其它控制器方法 實(shí)現(xiàn)自定義跳轉(zhuǎn)界面功能示例
這篇文章主要介紹了thinkphp5框架調(diào)用其它控制器方法 實(shí)現(xiàn)自定義跳轉(zhuǎn)界面功能,結(jié)合實(shí)例形式分析了thinkPHP5控制器調(diào)用、登陸判斷與界面跳轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
PHP面向?qū)ο缶幊讨钊肜斫夥椒ㄖ剌d與方法覆蓋(多態(tài))
這篇文章主要介紹了PHP面向?qū)ο缶幊讨钊肜斫夥椒ㄖ剌d與方法覆蓋(多態(tài))的相關(guān)資料,需要的朋友可以參考下2015-12-12
PHP服務(wù)器端API原理及示例講解(接口開發(fā))
下面小編就為大家分享一篇PHP服務(wù)器端API原理及示例講解(接口開發(fā)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-03-03
thinkPHP實(shí)現(xiàn)的省市區(qū)三級(jí)聯(lián)動(dòng)功能示例
這篇文章主要介紹了thinkPHP實(shí)現(xiàn)的省市區(qū)三級(jí)聯(lián)動(dòng)功能,詳細(xì)分析了thinkPHP實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能的詳細(xì)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

