Redis.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace crmeb\utils;
  3. use think\facade\Cache;
  4. /**
  5. * Redis 操作
  6. * Class Redis
  7. * @package crmeb\utils
  8. * @mixin \Redis
  9. */
  10. class Redis
  11. {
  12. /**
  13. * 实例化本身
  14. * @var object
  15. */
  16. protected static $instance;
  17. /**
  18. * redis
  19. * @var \think\cache\Driver\Redis
  20. */
  21. protected $redis;
  22. /**
  23. * 配置项
  24. * @var array
  25. */
  26. protected $options = [
  27. 'prefix' => '',
  28. ];
  29. /**
  30. * Redis constructor.
  31. */
  32. protected function __construct()
  33. {
  34. if (!extension_loaded('redis')) {
  35. throw new \BadFunctionCallException('not support: Redis');
  36. }
  37. $this->redis = Cache::store('redis');
  38. }
  39. /**
  40. * 实例化
  41. * @return Redis|object
  42. */
  43. public static function instance()
  44. {
  45. if (is_null(self::$instance)) self::$instance = new static();
  46. return self::$instance;
  47. }
  48. /**
  49. * 获取缓存前缀
  50. * @return mixed
  51. */
  52. public static function getPrefix()
  53. {
  54. return self::instance()->options['prefix'];
  55. }
  56. /**
  57. * 将多个数组出入到表头部
  58. * @param string $key
  59. * @param mixed ...$value
  60. * @return bool|int
  61. */
  62. public function lpush(string $key, ...$value)
  63. {
  64. return $this->redis->lPush($this->options['prefix'] . $key, ...$value);
  65. }
  66. public function rpush(string $key, ...$value)
  67. {
  68. return $this->redis->rPush($this->options['prefix'] . $key, ...$value);
  69. }
  70. /**
  71. * 移出并获取列表的第一个元素
  72. * @param string $key
  73. * @return string
  74. */
  75. public function lpop(string $key)
  76. {
  77. return $this->redis->lPop($this->options['prefix'] . $key);
  78. }
  79. public function rpop(string $key)
  80. {
  81. return $this->redis->rPop($this->options['prefix'] . $key);
  82. }
  83. /**
  84. * 获取长度
  85. * @param string $key
  86. * @return int
  87. */
  88. public function llen(string $key)
  89. {
  90. return $this->redis->lLen($this->options['prefix'] . $key);
  91. }
  92. /**
  93. * 获取数据
  94. * @param $key
  95. * @return mixed
  96. */
  97. public function get($key)
  98. {
  99. $func = function ($key) {
  100. return $this->options['prefix'] . $key;
  101. };
  102. if (is_array($key)) {
  103. $key = array_map($func, $key);
  104. return $this->redis->mget($key);
  105. } else {
  106. return $this->redis->get($this->options['prefix'] . $key);
  107. }
  108. }
  109. /**
  110. * 写入数据
  111. * @param string $key
  112. * @param $value
  113. * @param int $exprie
  114. * @return bool
  115. */
  116. public function set(string $key, $value, int $exprie = 0)
  117. {
  118. if ($exprie) {
  119. $res = $this->redis->setex($this->options['prefix'] . $key, $exprie, $value);
  120. } else {
  121. $res = $this->redis->set($this->options['prefix'] . $key, $value);
  122. }
  123. return $res;
  124. }
  125. /**
  126. * 静态调用
  127. * @param $name
  128. * @param $arguments
  129. * @return mixed
  130. */
  131. public static function __callStatic($name, $arguments)
  132. {
  133. if (method_exists(self::$instance->redis, $name)) {
  134. return self::instance()->redis->{$name}(...$arguments);
  135. }
  136. }
  137. }