Share The memcache cache methods in php and cache memcache. To share the memcache cache operations in php, you must configure the memcahche environment on the server before using memcache! After confirming that memcahce can be connected normally, you can operate the memcache cache method in php.
To use memcache, you must configure the memcahche environment on the server! After confirming that memcahce can be connected normally, it can be used in the program!
<? Php/*** Memcache cache operation * @ author hxm * @ version 1.0 * @ since 2015.05.04 */class MCache extends Object implements CacheFace {private $ mem = null; // Mem Object private $ sId = 1; // servier service ID/*** initialize Memcache ** @ return Object */public function _ construct () {if (! Class_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; // select if (! $ Server [$ sid]) {throw new QException ('Current cache server configuration file does not exist');} $ host = $ server [$ sid] ['host']; $ port = $ server [$ sid] ['port']; try {$ this-> mem-> connect ($ host, $ port);} catch (Exception $ e) {exit ('memecache connection failed, error message :'. $ e-> getMessage ());}} /*** write cache ** @ access private * @ param string $ key keyword * @ param string $ value cached content * @ return array */public function set ($ key, $ value, $ sid, $ expire = 0) {$ data = $ this-> get ($ key, $ sid); // if the key value if ($ data) already exists) {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 number of memcache servers * @ return array */public function get ($ key, $ sid) {$ this-> connect ($ sid); return $ t His-> 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 number of memcache servers * @ return array */public function remove ($ key, $ sid) {$ this-> connect (); return $ this-> mem-> delete ($ key );} /* destructor * finally disable memcache */public fun Ction _ destruct () {/* if (! $ This-> mem) {$ this-> mem-> close ();}*/}}
The above is all the content of this article. I hope you will like it.
Before using memcache, configure the memcahche environment on the server! After you confirm that memcahce can be connected normally, you can...