Skip to content

Commit 9c088f5

Browse files
committed
asterisk implies match for scanArgs
1 parent 9d50fb9 commit 9c088f5

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ redis-scan
2727
```
2828
Actually, there is a `eachLimit` (default 1000) so it will only scan a 1000 keys (in batches, with sleeps inbetween), and exit with an error message "Limit reached."
2929

30-
If the first parameter is a number, it is taken as the database number:
30+
If the first parameter is a number prefixed with `@` then it is taken as the database number:
3131
```shell
32-
redis-scan 2
32+
redis-scan @2
3333
```
3434
where this scans database number `2` via `redis-cli -n 2`
3535

3636
We can and should use `match` to reduce the number of keys.
3737
```shell
38-
redis-scan 0 match 'demo:*'
38+
redis-scan match 'demo:*'
3939
```
4040
If a parameter contains an asterisk, then `match` is assumed:
4141
```shell
@@ -55,13 +55,13 @@ where supported types are: `string, list, hash, set, zset.`
5555

5656
We can specify an "each" command to be executed for each matching key:
5757
```shell
58-
redis-scan 13 @hash -- hlen
58+
redis-scan @13 @hash -- hlen
5959
```
6060
where we use a double-dash to delimit the `each` command. In this case we execute `hlen` against each key of type `hash`
6161

6262
Actually the script knows that `hlen` is a hashes command, and so `@hash` can be omitted:
6363
```shell
64-
redis-scan 13 -- hlen
64+
redis-scan @13 -- hlen
6565
```
6666
where this will scan all keys in db `13,` and for each hashes key, print its `hlen.`
6767

bin/bashrc.redis-scan.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ RedisScan() { # scan command with sleep between iterations
5050
then
5151
scanCommand='scan'
5252
else
53-
if echo "$1" | grep -q "^[0-9][0-9]*$"
53+
if echo "$1" | grep -q "^[0-9][0-5]\{0,1\}$"
5454
then
55-
local dbn=$1
55+
rherror 'Use @dbn for the db number'
56+
local dbn="$1"
57+
shift
58+
redisArgs=" -n $dbn"
59+
rhinfo "dbn $dbn"
60+
elif echo "$1" | grep -q "^@[0-9][0-5]\{0,1\}$"
61+
then
62+
local dbn=`echo "$1" | tail -c+2`
5663
shift
5764
redisArgs=" -n $dbn"
5865
rhinfo "dbn $dbn"
@@ -200,6 +207,9 @@ RedisScan() { # scan command with sleep between iterations
200207
shift
201208
rhdebug eachCommand $eachCommand
202209
break
210+
elif printf '%s' "$arg" | grep -q '*'
211+
then
212+
scanArgs+=('match' "$arg")
203213
else
204214
scanArgs+=("$arg")
205215
fi

0 commit comments

Comments
 (0)