This article is a collection of classics by excerpts. Share the common theory with others.
Class rediscluster{
Whether to use M/s's read/write cluster scheme
Private isuseClus T e R = F a L s e ; / / S L a v e sentence Handle Mark Remember P R I v a T e _SN = 0;
Server connection handle
Private $_linkhandle = Array (
' Master ' =>null,//only supports one master
' Slave ' =>array (),//can have more than one slave
);
/** * Constructor * @param boolean $isUseCluster whether to use M/s scheme */public function __construct ($isUseCluster =false) {$this->_is Usecluster = $isUseCluster;} /** * Connect Server, Note: Use long connection here, improve efficiency, but do not automatically close * * @param array $config redis Server configuration * @param boolean $isMaster whether the currently added server is a Master server * @return Boolean */public function connect ($c $isMaster =true) {//default port if (!isset ($config [' Port '])} { $config [' port '] = 6379; }//Set Master connection if ($isMaster) {$this->_linkhandle[' Master '] = new Redis (); $ret = $this->_linkhandle[' master ']->pconnect ($config [' Host '], $config [' Port ']; }else{//Multiple Slave connection $this->_linkhandle[' Slave ' [$this->_sn] = new Redis (); $ret = $this->_linkhandle[' slave '] [$this->_sn]->pconnect ($config [' Host '), $config [' Port ']; + + $this->_sn; } return $ret;} /** * Close Connection * * @param int $flag off select 0: Off Master 1: Off Slave 2: Close all * @return Boolean */public function Close ($flag =2) { Switch ($flag) { Close Master case 0: $this->getredis ()->close (); Break Close Slave case 1:for ($i =0; $i < $this->_sn; + + $i) {$this->_linkhandle[' Slave '] [ $i]->close (); } break; Close all Case 1: $this->getredis ()->close (); for ($i =0; $i < $this->_sn; + + $i) {$this->_linkhandle[' slave '] [$i]->close (); } break; } return true; /** * Get Redis raw objects can have more action * * @param boolean $isMaster Returns the type of the server true: Returns Master false: Returns slave * @param boolean $slaveOne return Back to Slave Select true: Load balancer randomly returns a Slave select false: Returns all slave selections * @return Redis object */public function Getredis ($isMaster =true,$ Slave//returns only master if ($isMaster) {return $this->_linkhandle[' Master '); }else{return $slaveOne? $this->_getslaveredis (): $this->_linkhandle[' slave '; }}/** * Write Cache * * @param string $key Save key * @param string $valuE Cache value * @param int $expire Expiration time, 0: No Expiration time */public function set ($key, $value, $expire =0) {//Never Time out if ($expire = = 0) { $ret = $this->getredis ()->set ($key, $value); }else{$ret = $this->getredis ()->setex ($key, $expire, $value); } return $ret;} /** * Read Cache * * @param string $key cache key, supports fetching multiple $key at once = Array (' Key1 ', ' key2 ') * @return String | | The Boolean failure returns false, successfully returning the string */public function get ($key) {//is fetching multiple values at once $func = Is_array ($key)? ' MGet ': ' Get '; Not using M/s if (! $this->_isusecluster) {return $this->getredis ()->{$func} ($key); }//Use M/s return $this->_getslaveredis ()->{$func} ($key);} /** * Conditional Form Set cache, if key is not set, setting fails when set to exist * * @param string $key Cache key * @param string $value Cache value * @return Boolean */public Fu Nction setnx ($key, $value) {return $this->getredis ()->setnx ($key, $value);} /** * Delete Cache * * @param string | | Array $key cache key, supports single health: "Key1" or Multiple keys: Array (' Key1 ', ' key2 ') * @return int deleted number of Jian */public functiOn remove ($key) {//$key = "Key1" | | Array (' key1 ', ' Key2 ') return $this->getredis ()->delete ($key);} /** * value + + operation, similar to + + $i, if key does not exist automatically set to 0 after the Gaga operation * * @param string $key cache key * @param int $default operation default value * @return int The value after */public function incr ($key, $default =1) {if ($default = = 1) {return $this->getredis ()->incr ($key); }else{return $this->getredis ()->incrby ($key, $default); }}/** * Value reduction operation, similar-$i, if key does not exist automatically set to 0 after the subtraction operation * * @param string $key cache key * @param int $default operation default value * @return int The value after Operation */public function Decr ($key, $default =1) {if ($default = = 1) {return $this->getredis ()->DECR ($key); }else{return $this->getredis ()->decrby ($key, $default); }}/** * Add Empty current Database * * @return Boolean */public function Clear () {return $this->getredis ()->flushdb ();} /* =================== The following private method =================== *//** * Random HASH get Redis Slave Server handle * * @return Redis Object */private function _getslaverEdis () {//Slave directly returns if ($this->_sn <= 1) {return $this->_linkhandle[' Slave '][0]; }//Random Hash Gets the handle of the Slave $hash = $this->_hashid (Mt_rand (), $this->_sn); return $this->_linkhandle[' slave ' [$hash];} /** * The value between 0~m-1 after hash is obtained by ID * * @param string $id * @param int $m * @return int */private function _hashid ($id, $m =10) { Convert the string k to a value between 0~m-1 as the hash address of the corresponding record $k = MD5 ($id); $l = strlen ($k); $b = Bin2Hex ($k); $h = 0; for ($i =0; $i < $l; $i + +) {//Add mode hash $h + = substr ($b, $i *2,2); } $hash = ($h)% $m; return $hash;} /** * Lpush */public function Lpush ($key, $value) {return $this->getredis ()->lpush ($key, $value);} /** * Add Lpop */public function Lpop ($key) {return $this->getredis ()->lpop ($key);} /** * Lrange */public function Lrange ($key, $start, $end) {return $this->getredis ()->lrange ($key, $start, $end);} /** * Set Hash opeation */public function Hset ($name, $key, $value) {if (Is_arraY ($value)) {return $this->getredis ()->hset ($name, $key, serialize ($value)); } return $this->getredis ()->hset ($name, $key, $value);} /** * Get hash opeation */public function hget ($name, $key = null, $serialize =true) {if ($key) {$row = $this->g Etredis ()->hget ($name, $key); if ($row && $serialize) {unserialize ($row); } return $row; } return $this->getredis ()->hgetall ($name);} /** * Delete Hash opeation */public function Hdel ($name, $key = null) {if ($key) {return $this->getredis ()-> ; Hdel ($name, $key); } return $this->getredis ()->hdel ($name);} /** * Transaction start */public function multi () {return $this->getredis ()->multi ();} /** * Transaction send */public function exec () {return $this->getredis ()->exec ();}
}
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the Phpredis package class complete example, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.