Basic php methods for memcache caching

Source: Internet
Author: User
Tags delete cache
Basic php method for memcache operations complete instance for Memcache cache operations servier service ID
 Mem = new Memcache ();} /*** link to the memcahce service ** @ access private * @ param string $ key keyword * @ param string $ value cached content * @ return array * @ update http://www.lai18.com */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 ();}*/}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.