: This article describes how to operate redis Cache in php. if you are interested in the PHP Tutorial, refer to it.
ObjectPrivate $ sId = 1; // servier service ID private $ con = null; // link resource/*** initialize Redis ** @ return Object */public function _ construct () {if (! Class_exists ('redis ') {throw new QException('Php extension does not exist: redis') ;}$ this-> Redis = new redis ();} /*** 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('The current cache server configuration file does not exist');} $ host = $ server [$ sid] ['host']; $ port = $ server [$ sid] ['port']; try {$ this-> redis-> 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-> redis-> getset ($ key, $ value);} else {return $ this-> redis-> set ($ key, $ value );}} /*** 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 $ this-> redis-> get ($ key);}/*** clean (delete) all elements that have been stored ** @ access private * @ return array */public function flush () {$ this-> connect (); return $ this-> redis-> flushall ();} /*** 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-> redis-> del ($ key );} /* destructor * finally disable memcache */public function _ destruct () {if ($ this-> redis) {$ this-> redis-> close ();}}}
The above describes how to operate redis Cache in php, including require, Exception, and object content. I hope my friends who are interested in PHP will be helpful.