The premise of using Memcache is to configure the Memcahche environment first in the service side! Verify that the MEMCAHCE can be connected properly after the program is used!
<?php/** * Memcache Cache operation * @author HXM * @version 1.0 * @since 2015.05.04/class Mcache extends Object implemen TS Cacheface {private $mem = null;//mem Object Private $sId = 1; Servier Service ID/** * Initialization memcache * * @return Object */Public Function __construct () {if (!clas
S_exists (' Memcache ')) {throw new qexception (' PHP extension does not exist:memcache ');
} $this->mem = new Memcache (); /** * Link MEMCAHCE service * @access private * @param string $key keyword * @param string $value Cached content *
@return Array */Private Function Connect ($sid) {$file = $this->cachefile ();
Require $file;
if (! isset ($cache)) {throw new Qexception (' cache configuration file does not exist '. $file);
} $server = $cache [$this->cacheid]; $sid = Isset ($sid) = = 0?
$this->sid: $sid;//memcache Service Select if (! $server [$sid]) {throw new Qexception (' The cache server configuration file for the current operation does not exist '); } $host = $server [$sid] [' HOst '];
$port = $server [$sid] [' Port '];
try {$this->mem->connect ($host, $port);
The catch (Exception $e) {exit (' Memecache connection failed, error message: '. $e->getmessage ()); }/** * Write cache * @access Private * @param string $key keyword * @param string $value cache content * @ret Urn array/Public function set ($key, $value, $sid, $expire = 0) {$data = $this->get ($key, $sid);
The results already exist for the key value if ($data) {return $this->mem->set ($key, $value, memcache_compressed, $expire);
else {return $this->mem->add ($key, $value, memcache_compressed, $expire);
}/** * Read cache * @access private * @param string $key keyword * @param int $sid Select the first few memcache servers
* @return Array */Public function get ($key, $sid) {$this->connect ($SID);
return $this->mem->get ($key); /** * Clean (delete) All elements that have been stored * * @access Private * @return Array/PUBlic function Flush () {$this->connect ();
return $this->mem->flush (); /** * Delete Cache * * @access private * @param string $key keyword * @param int $sid Select the first few memcache servers * @re
Turn array */Public function remove ($key, $sid) {$this->connect ();
return $this->mem->delete ($key); /** * destructor * finally closes memcache/public Function __destruct () {/*if (! $this->mem) {$th
Is->mem->close ();
}*/
}
}
The above is the entire contents of this article, I hope you can enjoy.