Redis SCAN Command
The Redis SCAN command is used to iterate over the database keys in the database.
The SCAN command is a cursor-based iterator, which returns a new cursor to the user after each call. The user needs to use this new cursor as the cursor parameter for the next SCAN command to continue the iteration process.
SCAN returns an array containing two elements: the first element is the new cursor for the next iteration, and the second element is an array containing all the iterated elements. If the new cursor returns 0, it indicates that the iteration has ended.
Related commands:
- SSCAN command is used to iterate over elements in a set key.
- HSCAN command is used to iterate over key-value pairs in a hash key.
- ZSCAN command is used to iterate over elements in a sorted set (including element members and element scores).
Syntax
The basic syntax of the Redis SCAN command is as follows:
SCAN cursor [MATCH pattern] [COUNT count]
- cursor - The cursor.
- pattern - The matching pattern.
- count - Specifies how many elements to return from the dataset, with a default value of 10.
Available Versions
= 2.8.0
Return Value
Array list.
Example
redis 127.0.0.1:6379> scan 0 # Start a new iteration with 0 as the cursor
1) "17" # The cursor returned during the first iteration
2) 1) "key:12"
2) "key:8"
3) "key:4"
4) "key:14"
5) "key:16"
6) "key:17"
7) "key:15"
8) "key:10"
9) "key:3"
10) "key:7"
11) "key:1"
redis 127.0.0.1:6379> scan 17 # Start a new iteration with the cursor 17 returned from the first iteration
1) "0"
2) 1) "key:5"
2) "key:18"
3) "key:0"
4) "key:2"
5) "key:19"
6) "key:13"
7) "key:6"
8) "key:9"
9) "key:11"