RedisDriver.php 4.0 KB

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