Php redis Cache operations

Source: Internet
Author: User
Tags php redis delete cache
: 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.

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.