PHP Redis Distributed class

Source: Internet
Author: User

Configuration:

$redis _config = array (' prefix ' = ' ylmall_ ', ' master ' = = Array (' host ' = ' = ' 192.168.1.19 ', ' port ' = ' 6379 ',));

class file:

Class Cls_redis {/** * * @var $_configmaster master Config,default to empty array */private $_configmaster = Array ();/** * * @var $_configslave slave config,default to empty array */private $_configslave = Array ();/** * * @var $_rediskeyprefi  X the Redis key prefix */private $_rediskeyprefix = ";p rivate $debug = false;public $queries = Array ();/** * * @ignore * */function __construct ($redis _config) {$this->_configmaster = $redis _config [' Master '];//$this->_configslave = $redis _config[' slave '; $this->_rediskeyprefix = $redis _config [' prefix '];} /** * Get redis key Prefix * * @return string */public function Getkeyprefix () {return $this->_rediskeyprefix;} /** * Set redis key Prefix * * @return * */public function Setkeyprefix ($prefix) {$this->_rediskeyprefix = $prefix;} /** * * @var $_redismaster Redis master,default to null */private $_redismaster = null;/** * Get Redis master server. If Fail,throw errorexception. * * @return Redis */public function Getredismaster () {if ($this->_redismaster instanceof Redis) {return $this->_redismaster;} else {$this->_redismaster = new Redis (); try {$this->_redismaster->connect ($this->_configmaster [' Host '], $this->_configmaster [' Port ']); $this- >_redismaster->setoption (Redis::opt_prefix, $this->_rediskeyprefix);} catch (Exception $e) {//$this->errorshow (); throw new Errorexception ("Connect redis server". Implode (",", $this->_configmaster). "Fail!");} return $this->_redismaster;}} /** * * @var $_redisslave Redis slave,default to null */private $_redisslave = null;/** * Get Redis salve server. If Fail,throw a errorexception. * * @return Redis */public function Getredisslave () {if ($this->_redisslave instanceof Redis) {return $this->_rediss Lave;} else {$this->_redisslave = new Redis (); try {//$this->_redisslave->connect ($this->_configslave[' host '], $ this->_configslave[' Port ');//$this->_redisslave->setoption (Redis::opt_prefix, $this->_Rediskeyprefix); $this->_redisslave->connect ($this->_configmaster [' Host '], $this->_configmaster [' Port ']); $this->_redisslave->setoption (Redis::opt_prefix, $this->_rediskeyprefix);} catch (Exception $e) {//$this->errorshow ();//throw new Errorexception ("Connect redis server". Implode (",", $this->_configslave). "Fail!"); $this->errorshow (); throw new Errorexception ("Connect redis server". Implode (",", $this->_configmaster). "Fail!");} return $this->_redisslave;}} /** * * @var $_cmdscopemaster Master Sever command scope */private static $_cmdscopemaster = Array (' Multi ', ' exec ', ' Discar d ', ' watch ', ' unwatch ',//Key-value structure ' Setex ', ' Psetex ', ' setnx ', ' del ', ' delete ', ' incr ', ' Incrby ', ' incrbyfloat ' , ' decr ', ' Decrby ',//list structrue ' Lpush ', ' rpush ', ' lpushx ', ' rpushx ', ' lSet ', ' lrem ', ' lremove ', ' linsert ', ' LTrim ', ' Listtrim ',//Set Structrue ' Sadd ', ' Srem ', ' sremove ', ' smove ', ' sPop ',//hash structrue ' hset ', ' hsetnx ', ' Hdel ', ' Hincrby ' , ' Hincrbyfloat ', ' hmset ',//transaction ' multi ', ' exec ',//sorted set Structrue ' Zadd ', ' zdelete ', ' Zdeleterangebyrank ', ' zcount ', ' Zrange ', ' zrangebyscore ', ' expire ',//server ' info ');/** * Set master server COMMADN Scope * * @param array $cmds * @return void */public function Setcmdscopemaster (array $cmds) {Self::$_cmdscopemaster = Array_unique (Array_merge (self: : $_cmdscopemaster, $cmds));} /** * * @var $_cmdscopeslave Slave sever command scope */private static $_cmdscopeslave = Array (//Key-value structure ' Exists ', ' mGet ', ' getmultiple ',//list structure ' lpop ', ' rpop ', ' blpop ', ' brpop ', ' lsize ', ' lIndex ', ' lGet ', ' lrange ', ' Lgetrange ',//Set Structrue ' Sismember ', ' scontains ', ' scard ', ' ssize ', ' srandmember ', ' smembers ',//hash structrue ' Hgetall ', ' hget ', ' Hlen ', ' Hkeys ', ' hvals ', ' hexists ', ' hmget ',//sorted set Structrue ' Zrevrange ', ' zrevrangebyscore ');  * * Set Slave server COMMADN scope * * @param array $cmds * @return void */public function Setcmdscopeslave (array $CMDS) {Self::$_cmdscopeslave = Array_Unique (Array_merge (Self::$_cmdscopeslave, $cmds));} /** * Set a key value * * @param string $key * @param mixed $value * @param int $expire * @return BOOL */public function set ($key, $value, $expire = 0) {if ($this->debug) {$this->queries [] = "Custom set: $key $va Lue ";} $value = Serialize ($value), $this->getredismaster (), if ($expire) {return $this->_redismaster->setex ($key, $e Xpire, $value);} else {return $this->_redismaster->set ($key, $value);}} /** * Get the value of a key * * @param string $key * @return mixed */public function Get ($key) {if ($this->deb UG) {$this->queries [] = "Custom get: $key";} $this->getredisslave (); return unserialize ($this->_redisslave->get ($key));} /** * Call Redis method use master or slave instance. If Fail,throw a errorexception. * * @return * */public function __call ($name, $args) {if ($this->debug) {$this->queries [] = "Call method: $name" . Implode (', ', $args );} if (In_array ($name, Self::$_cmdscopemaster)) {$this->getredismaster (); return Call_user_func_array (Array ($this-& Gt;_redismaster, $name), $args);} ElseIf (In_array ($name, Self::$_cmdscopeslave)) {$this->getredisslave (); return Call_user_func_array (Array ($this ->_redisslave, $name), $args);} else {throw new Errorexception ("It is an invalidate method: {$name}!");}} /** * Set Redis resource to NULL when serializing */public function __sleep () {$this->_redismaster = $this->_redissl Ave = NULL;} /** * Set Redis resource to NULL when destruct */public function __destruct () {$this->_redismaster = $this->_redissl Ave = NULL;} Public Function Errorshow () {}}

  

PHP Redis Distributed class

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.