PHP內(nèi)存緩存Memcached類實例
更新時間:2014年12月08日 11:27:04 投稿:shichen2014
這篇文章主要介紹了PHP內(nèi)存緩存Memcached類,以實例形式分析了PHP內(nèi)存緩存Memcached的實現(xiàn)方法,是php操作memcached的典型應(yīng)用,非常具有實用價值,需要的朋友可以參考下
本文實例講述了PHP內(nèi)存緩存Memcached類。分享給大家供大家參考。
具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
<?PHP
class MemcacheModel {
private $mc = null;
/**
* 構(gòu)造方法,用于添加服務(wù)器并創(chuàng)建memcahced對象
*/
function __construct(){
$params = func_get_args();
$mc = new Memcache;
//如果有多個memcache服務(wù)器
if( count($params) > 1){
foreach ($params as $v){
call_user_func_array(array($mc, 'addServer'), $v);
}
//如果只有一個memcache服務(wù)器
} else {
call_user_func_array(array($mc, 'addServer'), $params[0]);
}
$this->mc=$mc;
}
/**
* 獲取memcached對象
* @return object memcached對象
*/
function getMem(){
return $this->mc;
}
/**
* 檢查mem是否連接成功
* @return bool 連接成功返回true,否則返回false
*/
function mem_connect_error(){
$stats=$this->mc->getStats();
if(emptyempty($stats)){
return false;
}else{
return true;
}
}
private function addKey($tabName, $key){
$keys=$this->mc->get($tabName);
if(emptyempty($keys)){
$keys=array();
}
//如果key不存在,就添加一個
if(!in_array($key, $keys)) {
$keys[]=$key; //將新的key添加到本表的keys中
$this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
return true; //不存在返回true
}else{
return false; //存在返回false
}
}
/**
* 向memcache中添加數(shù)據(jù)
* @param string $tabName 需要緩存數(shù)據(jù)表的表名
* @param string $sql 使用sql作為memcache的key
* @param mixed $data 需要緩存的數(shù)據(jù)
*/
function addCache($tabName, $sql, $data){
$key=md5($sql);
//如果不存在
if($this->addKey($tabName, $key)){
$this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0);
}
}
/**
* 獲取memcahce中保存的數(shù)據(jù)
* @param string $sql 使用SQL的key
* @return mixed 返回緩存中的數(shù)據(jù)
*/
function getCache($sql){
$key=md5($sql);
return $this->mc->get($key);
}
/**
* 刪除和同一個表相關(guān)的所有緩存
* @param string $tabName 數(shù)據(jù)表的表名
*/
function delCache($tabName){
$keys=$this->mc->get($tabName);
//刪除同一個表的所有緩存
if(!emptyempty($keys)){
foreach($keys as $key){
$this->mc->delete($key, 0); //0 表示立刻刪除
}
}
//刪除表的所有sql的key
$this->mc->delete($tabName, 0);
}
/**
* 刪除單獨(dú)一個語句的緩存
* @param string $sql 執(zhí)行的SQL語句
*/
function delone($sql){
$key=md5($sql);
$this->mc->delete($key, 0); //0 表示立刻刪除
}
}
?>
class MemcacheModel {
private $mc = null;
/**
* 構(gòu)造方法,用于添加服務(wù)器并創(chuàng)建memcahced對象
*/
function __construct(){
$params = func_get_args();
$mc = new Memcache;
//如果有多個memcache服務(wù)器
if( count($params) > 1){
foreach ($params as $v){
call_user_func_array(array($mc, 'addServer'), $v);
}
//如果只有一個memcache服務(wù)器
} else {
call_user_func_array(array($mc, 'addServer'), $params[0]);
}
$this->mc=$mc;
}
/**
* 獲取memcached對象
* @return object memcached對象
*/
function getMem(){
return $this->mc;
}
/**
* 檢查mem是否連接成功
* @return bool 連接成功返回true,否則返回false
*/
function mem_connect_error(){
$stats=$this->mc->getStats();
if(emptyempty($stats)){
return false;
}else{
return true;
}
}
private function addKey($tabName, $key){
$keys=$this->mc->get($tabName);
if(emptyempty($keys)){
$keys=array();
}
//如果key不存在,就添加一個
if(!in_array($key, $keys)) {
$keys[]=$key; //將新的key添加到本表的keys中
$this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
return true; //不存在返回true
}else{
return false; //存在返回false
}
}
/**
* 向memcache中添加數(shù)據(jù)
* @param string $tabName 需要緩存數(shù)據(jù)表的表名
* @param string $sql 使用sql作為memcache的key
* @param mixed $data 需要緩存的數(shù)據(jù)
*/
function addCache($tabName, $sql, $data){
$key=md5($sql);
//如果不存在
if($this->addKey($tabName, $key)){
$this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0);
}
}
/**
* 獲取memcahce中保存的數(shù)據(jù)
* @param string $sql 使用SQL的key
* @return mixed 返回緩存中的數(shù)據(jù)
*/
function getCache($sql){
$key=md5($sql);
return $this->mc->get($key);
}
/**
* 刪除和同一個表相關(guān)的所有緩存
* @param string $tabName 數(shù)據(jù)表的表名
*/
function delCache($tabName){
$keys=$this->mc->get($tabName);
//刪除同一個表的所有緩存
if(!emptyempty($keys)){
foreach($keys as $key){
$this->mc->delete($key, 0); //0 表示立刻刪除
}
}
//刪除表的所有sql的key
$this->mc->delete($tabName, 0);
}
/**
* 刪除單獨(dú)一個語句的緩存
* @param string $sql 執(zhí)行的SQL語句
*/
function delone($sql){
$key=md5($sql);
$this->mc->delete($key, 0); //0 表示立刻刪除
}
}
?>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP下escape解碼函數(shù)的實現(xiàn)方法
很多時候需要用到j(luò)s的escape函數(shù)來轉(zhuǎn)換中文字符,可是用js轉(zhuǎn)換后的字符怎么用php來轉(zhuǎn)換回來呢,下面我就找到了兩個很實用的函數(shù)。2010-08-08phpexcel導(dǎo)出excel的顏色和網(wǎng)頁中的顏色顯示不一致
關(guān)于phpexcel導(dǎo)出顏色的一些問題,用phpexcel做導(dǎo)出的excel的顏色怎么和網(wǎng)頁中的顏色顯示不一致呢,接下來將詳細(xì)介紹解決方法2012-12-12PHP 雜談《重構(gòu)-改善既有代碼的設(shè)計》之四 簡化條件表達(dá)式
條件邏輯有可能十分復(fù)雜,因此本章提供一些重構(gòu)的手法,專門用來簡化它們2012-04-04PHP XML error parsing SOAP payload on line 1
PHP中GBK頁面調(diào)用WebService的編碼問題:XML error parsing SOAP payload on line 12010-06-06PHP實現(xiàn)微信圖片上傳到服務(wù)器的方法示例
這篇文章主要介紹了PHP實現(xiàn)微信圖片上傳到服務(wù)器的方法,涉及php基于curl操作圖片文件的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-06-06