Redis.php 2.9 KB

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