PHP operation Redis Cache method sharing, Phpredis cache sharing
PHP Redis Cache Operations
<?php/** * Redis Cache operation * @author HXM * @version 1.0 * @since 2015.05.04 */class Rcache extends Object implements CACHEFAC e {private $redis = null;//redis Object Private $sId = 1; Servier Service ID Private $con = null;//Link Resource/** * Initialize Redis * * @return Object */Public function __construc T () {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 Cache contents * @return Array */Private Function Connect ($sid) {$file = $this->cachefile (); Require $file; if (! isset ($cache)) {throw new qexception (' cache config file does not exist '. $file); } $server = $cache [$this->cacheid]; $sid = Isset ($sid) = = 0? $this->sid: $sid;//memcache Service Select if (! $server [$sid]) {throw new Qexception (' Cache server configuration file for current operation 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 Cache contents * @return Array */Public function set ($key, $value, $sid, $expire = 0) {$data = $this->get ($key, $SID);//If a 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 memcache Server * @ret Urn 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->con Nect (); return $this->redis->flushall (); }/** * Delete Cache * * @accessPrivate * @param string $key keyword * @param int $sid Select memcache Server * @return Array */Public function remove ($key, $sid) {$this->connect (); return $this->redis->del ($key); }/** * destructor * Last Close memcache */Public Function __destruct () {if ($this->redis) {$this->redis ->close (); } }}
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1011246.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011246.html techarticle PHP operation Redis Cache method sharing, Phpredis cache sharing php Redis cache operation php/** * Redis Cache operation * @author HXM * @version 1.0 * @since 2015.05.04 */clas s Rcache extends OBJ ...