|
16 | 16 | use Hyperf\Pool\Connection as BaseConnection; |
17 | 17 | use Hyperf\Pool\Exception\ConnectionException; |
18 | 18 | use Hyperf\Pool\Pool; |
| 19 | +use Hyperf\Utils\Str; |
19 | 20 | use Psr\Container\ContainerInterface; |
20 | 21 |
|
21 | 22 | class RedisConnection extends BaseConnection implements ConnectionInterface |
@@ -56,6 +57,64 @@ public function __call($name, $arguments) |
56 | 57 | return $this->connection->{$name}(...$arguments); |
57 | 58 | } |
58 | 59 |
|
| 60 | + /** |
| 61 | + * @param null|int $cursor |
| 62 | + * @param string $pattern |
| 63 | + * @param int $count |
| 64 | + * |
| 65 | + * @return array|bool |
| 66 | + */ |
| 67 | + public function scan(&$cursor, $pattern = null, $count = 0) |
| 68 | + { |
| 69 | + $ret = $this->connection->scan($cursor, $this->applyPrefix($pattern), $count); |
| 70 | + if ($ret !== false) { |
| 71 | + $prefix = $this->applyPrefix(''); |
| 72 | + array_walk($ret, function (&$key) use ($prefix) { |
| 73 | + $key = Str::replaceFirst($prefix, '', $key); |
| 74 | + }); |
| 75 | + } |
| 76 | + return $ret; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param string $key |
| 81 | + * @param int $cursor |
| 82 | + * @param null|string $pattern |
| 83 | + * @param int $count |
| 84 | + * |
| 85 | + * @return array |
| 86 | + */ |
| 87 | + public function hScan($key, &$cursor, $pattern = null, $count = 0) |
| 88 | + { |
| 89 | + return $this->connection->hScan($key, $cursor, $pattern, $count); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param string $key |
| 94 | + * @param int $cursor |
| 95 | + * @param null|string $pattern |
| 96 | + * @param int $count |
| 97 | + * |
| 98 | + * @return array|bool |
| 99 | + */ |
| 100 | + public function zScan($key, &$cursor, $pattern = null, $count = 0) |
| 101 | + { |
| 102 | + return $this->connection->zScan($key, $cursor, $pattern, $count); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param string $key |
| 107 | + * @param int $cursor |
| 108 | + * @param null|string $pattern |
| 109 | + * @param int $count |
| 110 | + * |
| 111 | + * @return array|bool |
| 112 | + */ |
| 113 | + public function sScan($key, &$cursor, $pattern = null, $count = 0) |
| 114 | + { |
| 115 | + return $this->connection->sScan($key, $cursor, $pattern, $count); |
| 116 | + } |
| 117 | + |
59 | 118 | public function getActiveConnection() |
60 | 119 | { |
61 | 120 | if ($this->check()) { |
@@ -125,4 +184,17 @@ public function setDatabase(?int $database): void |
125 | 184 | { |
126 | 185 | $this->database = $database; |
127 | 186 | } |
| 187 | + |
| 188 | + /** |
| 189 | + * Apply prefix to the given key if necessary. |
| 190 | + * |
| 191 | + * @param string $key |
| 192 | + * |
| 193 | + * @return string |
| 194 | + */ |
| 195 | + private function applyPrefix($key = ''): string |
| 196 | + { |
| 197 | + $prefix = (string) $this->connection->getOption(\Redis::OPT_PREFIX); |
| 198 | + return $prefix . $key; |
| 199 | + } |
128 | 200 | } |
0 commit comments