Skip to content

Commit 5b55bb2

Browse files
author
yangliulnn
committed
Added *scan functions to Redis component.
1 parent 042e447 commit 5b55bb2

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/Redis.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ public function __call($name, $arguments)
6363
return $result;
6464
}
6565

66+
public function scan(&$cursor, $pattern = null, $count = 0)
67+
{
68+
$hasContextConnection = Context::has($this->getContextKey());
69+
$connection = $this->getConnection($hasContextConnection);
70+
return $connection->scan($cursor, $pattern, $count);
71+
}
72+
73+
public function hScan($key, &$cursor, $pattern = null, $count = 0)
74+
{
75+
$hasContextConnection = Context::has($this->getContextKey());
76+
$connection = $this->getConnection($hasContextConnection);
77+
return $connection->hScan($key, $cursor, $pattern, $count);
78+
}
79+
80+
public function zScan($key, &$cursor, $pattern = null, $count = 0)
81+
{
82+
$hasContextConnection = Context::has($this->getContextKey());
83+
$connection = $this->getConnection($hasContextConnection);
84+
return $connection->zScan($key, $cursor, $pattern, $count);
85+
}
86+
87+
public function sScan($key, &$cursor, $pattern = null, $count = 0)
88+
{
89+
$hasContextConnection = Context::has($this->getContextKey());
90+
$connection = $this->getConnection($hasContextConnection);
91+
return $connection->sScan($key, $cursor, $pattern, $count);
92+
}
93+
6694
/**
6795
* Define the commands that needs same connection to execute.
6896
* When these commands executed, the connection will storage to coroutine context.

src/RedisConnection.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\Pool\Connection as BaseConnection;
1717
use Hyperf\Pool\Exception\ConnectionException;
1818
use Hyperf\Pool\Pool;
19+
use Hyperf\Utils\Str;
1920
use Psr\Container\ContainerInterface;
2021

2122
class RedisConnection extends BaseConnection implements ConnectionInterface
@@ -56,6 +57,64 @@ public function __call($name, $arguments)
5657
return $this->connection->{$name}(...$arguments);
5758
}
5859

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+
59118
public function getActiveConnection()
60119
{
61120
if ($this->check()) {
@@ -125,4 +184,17 @@ public function setDatabase(?int $database): void
125184
{
126185
$this->database = $database;
127186
}
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+
}
128200
}

0 commit comments

Comments
 (0)