RedisDriver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Cache\Driver;
  12. use Hyperf\Cache\Exception\InvalidArgumentException;
  13. use Hyperf\Redis\Redis;
  14. use Hyperf\Redis\RedisFactory;
  15. use Psr\Container\ContainerInterface;
  16. class RedisDriver extends Driver implements KeyCollectorInterface
  17. {
  18. protected Redis $redis;
  19. public function __construct(ContainerInterface $container, array $config)
  20. {
  21. parent::__construct($container, $config);
  22. $this->redis = $container->get(RedisFactory::class)->get($config['options']['pool'] ?? 'default');
  23. }
  24. public function get($key, $default = null): mixed
  25. {
  26. $res = $this->redis->get($this->getCacheKey($key));
  27. if ($res === false) {
  28. return $default;
  29. }
  30. return $this->packer->unpack($res);
  31. }
  32. public function fetch(string $key, $default = null): array
  33. {
  34. $res = $this->redis->get($this->getCacheKey($key));
  35. if ($res === false) {
  36. return [false, $default];
  37. }
  38. return [true, $this->packer->unpack($res)];
  39. }
  40. public function set($key, $value, $ttl = null): bool
  41. {
  42. $seconds = $this->secondsUntil($ttl);
  43. $res = $this->packer->pack($value);
  44. if ($seconds > 0) {
  45. return $this->redis->set($this->getCacheKey($key), $res, $seconds);
  46. }
  47. return $this->redis->set($this->getCacheKey($key), $res);
  48. }
  49. public function delete($key): bool
  50. {
  51. return (bool) $this->redis->del($this->getCacheKey($key));
  52. }
  53. public function clear(): bool
  54. {
  55. return $this->clearPrefix('');
  56. }
  57. public function getMultiple($keys, $default = null): iterable
  58. {
  59. $cacheKeys = array_map(function ($key) {
  60. return $this->getCacheKey($key);
  61. }, $keys);
  62. $values = $this->redis->mget($cacheKeys);
  63. $result = [];
  64. foreach ($keys as $i => $key) {
  65. $result[$key] = $values[$i] === false ? $default : $this->packer->unpack($values[$i]);
  66. }
  67. return $result;
  68. }
  69. public function setMultiple($values, $ttl = null): bool
  70. {
  71. if (! is_array($values)) {
  72. throw new InvalidArgumentException('The values is invalid!');
  73. }
  74. $cacheKeys = [];
  75. foreach ($values as $key => $value) {
  76. $cacheKeys[$this->getCacheKey($key)] = $this->packer->pack($value);
  77. }
  78. $seconds = $this->secondsUntil($ttl);
  79. if ($seconds > 0) {
  80. foreach ($cacheKeys as $key => $value) {
  81. $this->redis->set($key, $value, $seconds);
  82. }
  83. return true;
  84. }
  85. return $this->redis->mset($cacheKeys);
  86. }
  87. public function deleteMultiple($keys): bool
  88. {
  89. $cacheKeys = array_map(function ($key) {
  90. return $this->getCacheKey($key);
  91. }, $keys);
  92. return (bool) $this->redis->del(...$cacheKeys);
  93. }
  94. public function has($key): bool
  95. {
  96. return (bool) $this->redis->exists($this->getCacheKey($key));
  97. }
  98. public function clearPrefix(string $prefix): bool
  99. {
  100. $iterator = null;
  101. $key = $prefix . '*';
  102. while (true) {
  103. $keys = $this->redis->scan($iterator, $this->getCacheKey($key), 10000);
  104. if (! empty($keys)) {
  105. $this->redis->del(...$keys);
  106. }
  107. if (empty($iterator)) {
  108. break;
  109. }
  110. }
  111. return true;
  112. }
  113. public function addKey(string $collector, string $key): bool
  114. {
  115. return (bool) $this->redis->sAdd($this->getCacheKey($collector), $key);
  116. }
  117. public function keys(string $collector): array
  118. {
  119. return $this->redis->sMembers($this->getCacheKey($collector));
  120. }
  121. public function delKey(string $collector, string ...$key): bool
  122. {
  123. return (bool) $this->redis->sRem($this->getCacheKey($collector), ...$key);
  124. }
  125. public function getConnection(): mixed
  126. {
  127. return $this->redis;
  128. }
  129. }