123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- declare (strict_types = 1);
- namespace library\utils;
- class RedisCli {
-
- private $_isUseCluster = false;
-
- private $_sn = 0;
-
- private $_linkHandle = array(
- 'master' => null,
- 'slave' => array(),
- );
-
- public function __construct($isUseCluster = false) {
- $this->_isUseCluster = $isUseCluster;
- }
-
- public function connect($config = array('host' => '127.0.0.1', 'port' => 6379), $auth = array(), $isMaster = true) {
-
- if (!isset($config['port'])) {
- $config['port'] = 6379;
- }
-
- if ($isMaster) {
- $this->_linkHandle['master'] = new \Redis();
- $ret = $this->_linkHandle['master']->pconnect($config['host'], $config['port']);
- if (count($auth) > 0 && !empty($auth[0])) {
- if ($this->_linkHandle['master']->auth($auth[0] . ":" . $auth[1]) === false) {
- die('Redis PassWord Error');
- }
- }
- } else {
-
- $this->_linkHandle['slave'][$this->_sn] = new Redis();
- $ret = $this->_linkHandle['slave'][$this->_sn]->pconnect($config['host'], $config['port']);
- if (count($auth) > 0 && !empty($auth[0])) {
- if ($this->_linkHandle['master']->auth($auth[0] . ":" . $auth[1]) === false) {
- die('Redis PassWord Error');
- }
- }
- ++$this->_sn;
- }
- return $ret;
- }
-
- public function close($flag = 2) {
- switch ($flag) {
-
- case 0:
- $this->getRedis()->close();
- break;
-
- case 1:
- for ($i = 0; $i < $this->_sn; ++$i) {
- $this->_linkHandle['slave'][$i]->close();
- }
- break;
-
- case 1:
- $this->getRedis()->close();
- for ($i = 0; $i < $this->_sn; ++$i) {
- $this->_linkHandle['slave'][$i]->close();
- }
- break;
- }
- return true;
- }
-
- public function getRedis($isMaster = true, $slaveOne = true) {
-
- if ($isMaster) {
- return $this->_linkHandle['master'];
- } else {
- return $slaveOne ? $this->_getSlaveRedis() : $this->_linkHandle['slave'];
- }
- }
-
- public function set($key, $value, $expire = 0) {
-
- if ($expire == 0) {
- $ret = $this->getRedis()->set($key, $value);
- } else {
- $ret = $this->getRedis()->setex($key, $expire, $value);
- }
- return $ret;
- }
-
- public function get($key) {
-
- $func = is_array($key) ? 'mGet' : 'get';
-
- if (!$this->_isUseCluster) {
- return $this->getRedis()->{$func}($key);
- }
-
- return $this->_getSlaveRedis()->{$func}($key);
- }
-
-
- public function setnx($key, $value) {
- return $this->getRedis()->setnx($key, $value);
- }
-
- public function remove($key) {
-
- return $this->getRedis()->delete($key);
- }
-
- public function incr($key, $default = 1) {
- if ($default == 1) {
- return $this->getRedis()->incr($key);
- } else {
- return $this->getRedis()->incrBy($key, $default);
- }
- }
-
- public function decr($key, $default = 1) {
- if ($default == 1) {
- return $this->getRedis()->decr($key);
- } else {
- return $this->getRedis()->decrBy($key, $default);
- }
- }
-
- public function clear() {
- return $this->getRedis()->flushDB();
- }
-
-
- private function _getSlaveRedis() {
-
- if ($this->_sn <= 1) {
- return $this->_linkHandle['slave'][0];
- }
-
- $hash = $this->_hashId(mt_rand(), $this->_sn);
- return $this->_linkHandle['slave'][$hash];
- }
-
- private function _hashId($id, $m = 10) {
-
- $k = md5($id);
- $l = strlen($k);
- $b = bin2hex($k);
- $h = 0;
- for ($i = 0; $i < $l; $i++) {
-
- $h += substr($b, $i * 2, 2);
- }
- $hash = ($h * 1) % $m;
- return $hash;
- }
-
- public function lpush($key, $value) {
- return $this->getRedis()->lpush($key, $value);
- }
-
- public function lpop($key) {
- return $this->getRedis()->lpop($key);
- }
-
- public function lrange($key, $start, $end) {
- return $this->getRedis()->lrange($key, $start, $end);
- }
-
- 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);
- }
-
- public function hget($name, $key = null, $serialize = true) {
- if ($key) {
- $row = $this->getRedis()->hget($name, $key);
- if ($row && $serialize) {
- unserialize($row);
- }
- return $row;
- }
- return $this->getRedis()->hgetAll($name);
- }
-
- public function hdel($name, $key = null) {
- if ($key) {
- return $this->getRedis()->hdel($name, $key);
- }
- return $this->getRedis()->hdel($name);
- }
-
- public function multi() {
- return $this->getRedis()->multi();
- }
-
- public function exec() {
- return $this->getRedis()->exec();
- }
- }
|