<? Php /** * Redis cache operations * @ 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 to the 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 Selection 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 already exists If ($ data) { 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 * Disable memcache. */ Public function _ destruct () { If ($ this-> redis) { $ This-> redis-> close (); } } } |