/** * Memcache cache operations * @ 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 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-> 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 already exists 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 number of 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 number of memcache servers * @ Return array */ Public function remove ($ key, $ sid) { $ This-> connect (); Return $ this-> mem-> delete ($ key ); } /** * Destructor * Disable memcache. */ Public function _ destruct () { /* If (! $ This-> mem) { $ This-> mem-> close (); }*/ } } |