RedisDataServer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Servers;
  3. use Illuminate\Support\Facades\Redis;
  4. /**
  5. * Redis数据缓存类
  6. */
  7. class RedisDataServer
  8. {
  9. /**
  10. * 单列对象
  11. * @var
  12. */
  13. private static $server;
  14. private function __construct()
  15. {
  16. }
  17. /**
  18. * 创建对象
  19. * @return RedisDataServer
  20. */
  21. static function creatServer()
  22. {
  23. if (empty(self::$server)) {
  24. self::$server = new RedisDataServer();
  25. }
  26. return self::$server;
  27. }
  28. /**
  29. * 获取缓存数据
  30. * @param $key
  31. * @param string $type
  32. * @return mixed
  33. */
  34. function getData($key, $type = 'str')
  35. {
  36. $value = Redis::get($key);
  37. if ($type == 'json') {
  38. $value = json_decode($value, true);
  39. }
  40. return $value;
  41. }
  42. /**
  43. * 设置缓存数据
  44. * @param $key
  45. * @param $value
  46. * @param string $type
  47. * @param int $time
  48. * @param $random
  49. */
  50. function setData($key, $value, $type = 'str', $time = 10, $random = true)
  51. {
  52. if (!empty($value)) {
  53. if ($type == 'json') {
  54. $value = json_encode($value);
  55. }
  56. if ($time > 1 && $random) $time += $this->getRandom();
  57. Redis::setex($key, $time, $value);
  58. }
  59. }
  60. /**
  61. * 长时间存放数据
  62. * @param $key
  63. * @param $value
  64. * @param string $type
  65. */
  66. function setLongData($key, $value, $type = 'str')
  67. {
  68. if (!empty($value)) {
  69. if ($type == 'json') {
  70. $value = json_encode($value);
  71. }
  72. Redis::set($key, $value);
  73. }
  74. }
  75. /**
  76. * 更新数据
  77. * @param $key
  78. * @param $value
  79. * @param string $type
  80. * @param int $start_num
  81. */
  82. function setSetrange($key, $value, $type = 'str', $start_num = 0)
  83. {
  84. if (!empty($value)) {
  85. if ($type == 'json') {
  86. $value = json_encode($value);
  87. }
  88. Redis::setrange($key, $start_num, $value);
  89. }
  90. }
  91. /**
  92. * 删除Redis数据
  93. * @param $key
  94. */
  95. function delData($key)
  96. {
  97. Redis::del($key);
  98. }
  99. /**
  100. * 获取随机数
  101. * @return int
  102. */
  103. private function getRandom()
  104. {
  105. return mt_rand(1, 10);
  106. }
  107. /**
  108. * 创建队列序号
  109. * @param $key
  110. * @param $num
  111. * @return mixed
  112. */
  113. function setQueue($key, $num)
  114. {
  115. $num_arr = range(1, $num);
  116. $key_num = Redis::lpush($key, $num_arr);
  117. return $key_num;
  118. }
  119. /**
  120. * 添加序号
  121. * @param $key
  122. * @param $num
  123. * @return mixed
  124. */
  125. function addQueue($key, $num)
  126. {
  127. $key_num = Redis::lpush($key, $num);
  128. return $key_num;
  129. }
  130. /**
  131. * 弹出队列数据
  132. * @param $key
  133. * @return false
  134. */
  135. function getQueue($key)
  136. {
  137. $key_num = Redis::rpop($key);
  138. if (empty($key_num)) $key_num = false;
  139. return $key_num;
  140. }
  141. /**
  142. * 队列阻塞弹出
  143. * @param $key
  144. * @param int $wait_time
  145. * @return false
  146. */
  147. function getWaitQueue($key, $wait_time = 5)
  148. {
  149. $key_num = Redis::brpop($key, $wait_time);
  150. if (empty($key_num)) $key_num = false;
  151. return $key_num;
  152. }
  153. /**
  154. * 获取队列数据长度
  155. * @param $key
  156. * @return mixed
  157. */
  158. function getQueueList($key)
  159. {
  160. $key_num = Redis::llen($key);
  161. return $key_num;
  162. }
  163. /**
  164. * 获取过期时间
  165. * @param $key
  166. * @return mixed
  167. */
  168. function getTime($key)
  169. {
  170. $key_num = Redis::ttl($key);
  171. return $key_num;
  172. }
  173. /**
  174. * 缓存清除
  175. * @return mixed
  176. */
  177. function flushdb()
  178. {
  179. return Redis::flushdb();
  180. }
  181. /**
  182. * key加锁
  183. * @param $key
  184. * @param $value
  185. * @param int $time
  186. * @return mixed
  187. */
  188. function lockKey($key,$value,$time = 10){
  189. return Redis::set($key, $value, 'ex', $time, 'nx');
  190. }
  191. /**
  192. * 解锁
  193. * @param $key
  194. * @param $value
  195. * @return mixed
  196. */
  197. function unLockKey($key, $value)
  198. {
  199. $res = $this->getData($key);
  200. if ($res == $value) {
  201. return Redis::del($key);
  202. }
  203. }
  204. }