PHP Memory Cache memcached Class code

Source: Internet
Author: User
  code is as follows copy code

? Php

Class Memcachemodel {
Private $MC = null;
/**
* Constructs a method for adding servers and creating memcahced objects
*/
function __construct () {
$params = Func_get_args ();
$MC = new Memcache;
If you have 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 objects
*/
function Getmem () {
return $this->mc;
}
/**
* Check if MEM is connected successfully
* @return BOOL Connection successfully returns TRUE, otherwise 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 key does not exist, add a
if (!in_array ($key, $keys)) {
$keys []= $key; Add a new key to the keys in this table
$this->mc->set ($tabName, $keys, memcache_compressed, 0);
return true; Does not exist returns true
}else{
return false; Existence returns false
}
}
/**
* Add data to the Memcache
* @param string $tabName table name to cache the datasheet
* @param string $sql Use SQL as key for Memcache
* @param mixed $data data that needs to be cached
*/
function Addcache ($tabName, $sql, $data) {
$key =md5 ($sql);
If there is no
if ($this->addkey ($tabName, $key)) {
$this->mc->set ($key, $data, memcache_compressed, 0);
}
}
/**
* Get the data saved in the MEMCAHCE
* @param string $sql key with SQL
* @return Mixed returns the data in the cache
*/
function GetCache ($sql) {
$key =md5 ($sql);
return $this->mc->get ($key);
}


/**
* Deletes all caches related to the same table
* @param  string  $tabName   Datasheet table name
/
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 to delete immediately
}
}
//delete all SQL key
$this->mc->delete of the table ($ TabName, 0);
}
/**
* Delete the cache for a single statement
* @param  string  $sql executed SQL statement
*/
Function DeLone ($sql) {
$key =m D5 ($sql);
$this->mc->delete ($key, 0);//0 means delete immediately
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.