欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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 表示立刻刪除
}
}
?>

希望本文所述對大家的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論