Redis.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dongdx
  5. * Date: 2017/6/21
  6. * Time: 10:25
  7. */
  8. namespace Heanup\Library;
  9. use Heanup\Config\Redis\Core;
  10. use Heanup\Frame\Logger;
  11. class Redis
  12. {
  13. private $cfg;
  14. private $w_redis;
  15. private $r_redis;
  16. public static $instance;
  17. public function __construct()
  18. {
  19. $this->cfg = Core::getData();
  20. }
  21. public static function instance(): Redis
  22. {
  23. if (!isset(self::$instance)) {
  24. $instance = new self();
  25. self::$instance = $instance;
  26. } else {
  27. $instance = self::$instance;
  28. }
  29. return $instance;
  30. }
  31. public function get($key)
  32. {
  33. $redis = $this->connect(true);
  34. if (empty($key)) {
  35. return '';
  36. } else {
  37. $res = $redis->get($key);
  38. $row = json_decode($res, true);
  39. if ($res == '[]') $res = array();
  40. return $row ?: $res;
  41. }
  42. }
  43. public function set($key, $value, $life_time_limit = 0)
  44. {
  45. $redis = $this->connect(false);
  46. if (is_array($value)) {
  47. $value = json_encode($value);
  48. }
  49. if ($life_time_limit) {
  50. return $redis->setex($key, $life_time_limit, $value);
  51. } else {
  52. return $redis->set($key, $value);
  53. }
  54. }
  55. public function del($key)
  56. {
  57. $redis = $this->connect(false);
  58. return $redis->del($key);
  59. }
  60. public function hExists($key, $hashkey)
  61. {
  62. $redis = $this->connect(true);
  63. return boolval($redis->hExists($key, $hashkey));
  64. }
  65. public function hSet($key, $hashkey, $value)
  66. {
  67. $redis = $this->connect(false);
  68. if (is_array($value)) {
  69. $value = json_encode($value);
  70. }
  71. return $redis->hSet($key, $hashkey, $value);
  72. }
  73. public function hDel($key, $hashkey)
  74. {
  75. $redis = $this->connect(false);
  76. return $redis->hDel($key, $hashkey);
  77. }
  78. public function hGet($key, $hashkey)
  79. {
  80. $redis = $this->connect(true);
  81. $res = $redis->hGet($key, $hashkey);
  82. $row = json_decode($res, true);
  83. if ($res == '[]') $res = array();
  84. return $row ?: $res;
  85. }
  86. public function hLen($key)
  87. {
  88. $redis = $this->connect(true);
  89. $res = $redis->hLen($key);
  90. return intval($res);
  91. }
  92. public function sAdd($key, $values)
  93. {
  94. $redis = $this->connect(false);
  95. if (is_array($values)) {
  96. $res = $redis->sAddArray($key,$values);
  97. } else if(is_scalar($values)) {
  98. $res = $redis->sAdd($key, $values);
  99. }
  100. return $res;
  101. }
  102. public function sRem($key,$value)
  103. {
  104. $redis = $this->connect(false);
  105. return $redis->sRem($key,$value);
  106. }
  107. public function sMembers($key)
  108. {
  109. $redis = $this->connect(true);
  110. return $redis->sMembers($key);
  111. }
  112. public function hMGet($key, $hashkey)
  113. {
  114. $redis = $this->connect(true);
  115. return $redis->hMGet($key, $hashkey);
  116. }
  117. public function hGetAll($key)
  118. {
  119. $redis = $this->connect(true);
  120. return $redis->hGetAll($key);
  121. }
  122. public function hVals($key)
  123. {
  124. $redis = $this->connect(true);
  125. return $redis->hVals($key);
  126. }
  127. public function delete($key)
  128. {
  129. $redis = $this->connect(false);
  130. return $redis->delete($key);
  131. }
  132. public function incr($key)
  133. {
  134. $redis = $this->connect(false);
  135. return $redis->incr($key);
  136. }
  137. public function multi()
  138. {
  139. $redis = $this->connect(false);
  140. $redis ->multi();
  141. }
  142. public function exec()
  143. {
  144. $redis = $this->connect(false);
  145. try {
  146. $result = $redis ->exec();
  147. return $result;
  148. } catch (\Exception $ex) {
  149. Logger::error($ex->getMessage()."-".$ex->getTraceAsString()."(".$ex->getFile().":".$ex->getFile().")");
  150. // return false;
  151. }
  152. }
  153. /**
  154. * @param bool $is_read
  155. * @return bool|\Redis
  156. */
  157. public function connect($is_read = false)
  158. {
  159. try {
  160. if ($is_read) {
  161. //获取redis读连接
  162. if ($this->r_redis) {
  163. return $this->r_redis;
  164. }
  165. $this->r_redis = new \Redis();
  166. $config = $this->cfg['slave_list'][0];
  167. if (isset($config['pconnect']) && $config['pconnect'] == 1) {
  168. $this->r_redis->pconnect($config['host'], $config['port'], $config['timeout']);
  169. } else {
  170. $this->r_redis->connect($config['host'], $config['port'], $config['timeout']);
  171. }
  172. if (isset($config['password']) && $config['password']) {
  173. $this->r_redis->auth($config['password']);
  174. }
  175. //选择redis-db -by edison
  176. if (isset($config['db']))
  177. $this->r_redis->select(intval($config['db']));
  178. return $this->r_redis;
  179. } else {
  180. //获取redis写连接
  181. if ($this->w_redis) {
  182. return $this->w_redis;
  183. }
  184. $this->w_redis = new \Redis();
  185. $config = $this->cfg['master'];
  186. if (isset($config['pconnect']) && $config['pconnect'] == 1) {
  187. $this->w_redis->pconnect($config['host'], $config['port'], $config['timeout']);
  188. } else {
  189. $this->w_redis->connect($config['host'], $config['port'], $config['timeout']);
  190. }
  191. if (isset($config['password']) && $config['password']) {
  192. $this->w_redis->auth($config['password']);
  193. }
  194. //选择redis-db -by edison
  195. if (isset($config['db']))
  196. $this->w_redis->select(intval($config['db']));
  197. return $this->w_redis;
  198. }
  199. } catch (\Exception $ex) {
  200. $message = "Redis connection failed : [" . $config['host'] . ';' . $config['port'] . ']' . $ex->getMessage();
  201. Logger::error($message);
  202. // return false;
  203. }
  204. }
  205. }