Proxy.php 6.1 KB

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