Home > Backend Development > PHP Tutorial > PHP memory cache Memcached class code_PHP tutorial

PHP memory cache Memcached class code_PHP tutorial

WBOY
Release: 2016-07-20 11:11:11
Original
816 people have browsed it

Friends who need the PHP memory cache Memcached class can refer to it.

The code is as follows
 代码如下 复制代码


class MemcacheModel {
private $mc = null;
/**
* 构造方法,用于添加服务器并创建memcahced对象
*/
function __construct(){
$params = func_get_args();
$mc = new Memcache;
//如果有多个memcache服务器
if( count($params) > 1){
foreach ($params as $v){
call_user_func_array(array($mc, 'addServer'), $v);
}
//如果只有一个memcache服务器
} 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(empty($stats)){
return false;
}else{
return true;
}
}

private function addKey($tabName, $key){
$keys=$this->mc->get($tabName);
if(empty($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中添加数据
* @param string $tabName 需要缓存数据表的表名
* @param string $sql 使用sql作为memcache的key
* @param mixed $data 需要缓存的数据
*/
function addCache($tabName, $sql, $data){
$key=md5($sql);
//如果不存在
if($this->addKey($tabName, $key)){
$this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0);
}
}
/**
* 获取memcahce中保存的数据
* @param string $sql 使用SQL的key
* @return mixed 返回缓存中的数据
*/
function getCache($sql){
$key=md5($sql);
return $this->mc->get($key);
}


/**
* 删除和同一个表相关的所有缓存
* @param string $tabName 数据表的表名
*/
function delCache($tabName){
$keys=$this->mc->get($tabName);
//删除同一个表的所有缓存
if(!empty($keys)){
foreach($keys as $key){
$this->mc->delete($key, 0); //0 表示立刻删除
}
}
//删除表的所有sql的key
$this->mc->delete($tabName, 0);
}
/**
* 删除单独一个语句的缓存
* @param string $sql 执行的SQL语句
*/
function delone($sql){
$key=md5($sql);
$this->mc->delete($key, 0); //0 表示立刻删除
}
}

Copy code

class MemcacheModel {
private $mc = null;
/**
* Construction method, used to add servers and create memcahced objects
*/
function __construct(){
$params = func_get_args();
$mc = new Memcache;
//If there are multiple memcache servers
if( count($params) > 1){foreach ($params as $v){
call_user_func_array(array($mc, 'addServer'), $v);
}
//If there is only one memcache server
} else {
call_user_func_array(array($mc, 'addServer'), $params[0]);
}
$this->mc=$mc;
}
/**
* Get memcached object
* @return object memcached object
*/
function getMem(){
return $this->mc;
}
/**
* Check whether mem is connected successfully
* @return bool If the connection is successful, it returns true, otherwise it returns false
*/
function mem_connect_error(){
$stats=$this->mc->getStats();
if(empty($stats)){
return false ;
}else{
return true;
}
}
private function addKey($tabName, $key){
$keys=$ this->mc->get($tabName);
if(empty($keys)){
$keys=array();
}
//if If the key does not exist, add one
if(!in_array($key, $keys)) {
$keys[]=$key; //Add the new key to the keys of this table$this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
return true; //If it does not exist, return true
}else{
return false; //Exists and returns false
}
}
/**
* Add data to memcache
* @param string $tabName The table name of the data table that needs to be cached
* @param string $sql Use sql as the key of memcache
* @param mixed $data Data that needs to be cached
*/
function addCache($tabName, $sql, $data){
$key=md5($sql);
//If it does not exist
if($this->addKey($tabName, $key)){
$this->mc- >set($key, $data, MEMCACHE_COMPRESSED, 0);
}
}
/**
* Get the data saved in memcahce
* @param string $sql Use SQL key
* @return mixed Return the data in the cache
*/
function getCache($sql){
$key=md5($sql);
return $this->mc->get($key);
}


/**
* Delete all caches related to the same table
* @param string $tabName The table name of the data table
*/
function delCache($tabName){
$keys=$this->mc->get($tabName);
//Delete all caches of the same table
if(!empty($keys)){
foreach($keys as $key){
$this->mc->delete($key, 0); // 0 means delete immediately
}
}
//Delete all sql keys of the table
$this->mc->delete($tabName, 0);
/>}
/**
* Delete the cache of a single statement
* @param string $sql SQL statement executed
*/
function delone($sql){
$key=md5($sql);
$this->mc- >delete($key, 0); //0 means delete immediately
}
} http://www.bkjia.com/PHPjc/444667.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/444667.htmlTechArticleFriends in need of the PHP memory cache Memcached class can refer to it. The code is as follows Copy the code ?PHP class MemcacheModel { private $mc = null; /** * Construction method, used to add servers and create...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template