Home > Backend Development > PHP Tutorial > PHP memory cache Memcached class example, phpmemcached_PHP tutorial

PHP memory cache Memcached class example, phpmemcached_PHP tutorial

WBOY
Release: 2016-07-13 10:12:11
Original
992 people have browsed it

PHP memory cache Memcached class instance, phpmemcached

The example in this article describes the PHP memory cache Memcached class. Share it with everyone for your reference.

The specific implementation method is as follows:

Copy code The code is as follows:
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 if mem is connected successfully
* @return bool Returns true if the connection is successful, otherwise returns 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();
}
//If the key does not exist, add one
if(!in_array($key, $keys)) {
$keys[]=$key; //Add new keys to the keys of this table
$this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
return true; // does not exist return true
}else{
return false; //Exists return false
}
}
/**
* Add data to memcache
* @param string $tabName The name of the data table that needs to be cached
* @param string $sql Use sql as the key of memcache
* @param mixed $data Data 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 returns 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(!emptyempty($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
}
}
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/923901.htmlTechArticlePHP memory cache Memcached class example, phpmemcached This article describes the PHP memory cache Memcached class. Share it with everyone for your reference. The specific implementation method is as follows: Copy the code The code is as follows...
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