PHP Database Cache Memcache Operations class

Source: Internet
Author: User
Tags php database

example, the code is as follows

The code is as follows Copy Code

Class Memcachedinit {
Private $memcache;

/**
* Memcache Cache-Set Cache
* Set cache Key,value and cache time
* @param string $key key value
* @param string $value value
* @param string $time cache time
*/
Public Function Set_cache ($key, $value, $time = 0) {
return $this->memcache->set ($key, $value, False, $time);
}

/**
* Memcache Cache-Get Cache
* Get cached data through key
* @param string $key key value
*/
Public Function Get_cache ($key) {
return $this->memcache->get ($key);
}

/**
* Memcache Cache-Clears a cache
* Remove a cache from the Memcache
* @param string $key key value
*/
Public function Clear ($key) {
return $this->memcache->delete ($key);
}

/**
* Memcache Cache-Empty all caches
* It is not recommended to use this feature
* @return
*/
Public Function Clear_all () {
return $this->memcache->flush ();
}

/**
* Field self-used for counting
* @param string $key key value
* @param int $step The new step value
*/
Public function Increment ($key, $step = 1) {
return $this->memcache->increment ($key, (int) $step);
}

/**
* Field self-minus-for counting
* @param string $key key value
* @param int $step The new step value
*/
Public function Decrement ($key, $step = 1) {
return $this->memcache->decrement ($key, (int) $step);
}

/**
* Close Memcache Link
*/
Public function Close () {
return $this->memcache->close ();
}

/**
* Replace data
* @param string $key The data expected to be replaced
* @param string $value the replaced value
* @param int $time time value
* @param bool $flag whether to compress
*/
Public function replace ($key, $value, $time = 0, $flag = False) {
return $this->memcache->replace ($key, $value, False, $time);
}

/**
* Get the Memcache version number
*/
Public Function getversion () {
return $this->memcache->getversion ();
}

/**
* Get Memcache's state data
*/
Public Function GetStats () {
return $this->memcache->getstats ();
}

/**
* Memcache Caching-setting up a linked server
* Support multiple Memcache servers
* Configure the Memcache cache server in the configuration file:
* $InitPHP _conf[' memcache '][0] = array (' 127.0.0.1 ', ' 11211 ');
* @param array $servers server arrays-array (' 127.0.0.1 ', ' 11211 '))
*/
Public Function Add_server ($servers) {
$this->memcache = new Memcache;
if (!is_array ($servers) | | empty ($servers)) exit (' Memcache server is null! ');
foreach ($servers as $val) {
$this->memcache->addserver ($val [0], $val [1]);
}
}
}

How to use

$newclass = new Memcachedinit ();
$newclass->getversion ()//Get version number
$newclass->close ()//close Memcache link
$newclass->clear ($key)//Remove a cache from Memcache
$newclass->get_cache ($key)//Get cached data via key

The above is a brief introduction of its use of the method, in fact, there are many here I do not introduce AH.

To sum up, this is just one of the most based on the Memcache caching operations class, compared to the database caching class operations will be better and more complex oh, I hope the example can help friends oh.

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.