Share redis cache methods in php and phpredis Cache
Php redis cache operations
<? Php/*** Redis cache operation ** @ author hxm * @ version 1.0 * @ since 2015.05.04 */class RCache extends Object implements CacheFace {private $ redis = null; // redis object private $ 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 ('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 is all the content of this article. I hope you will like it.