Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis SCAN Command

Redis SCAN Command

Redis Key

The Redis Scan command is used to iterate over the keys in the database.

The SCAN command is a cursor-based iterator. Each time it is called, it returns a new cursor to the user; the user must use this new cursor as the cursor argument of the next SCAN command to continue the iteration.

SCAN returns an array with two elements: the first is the new cursor for the next iteration, and the second is an array containing all the elements iterated so far. A returned cursor of 0 means the iteration is finished.

Related commands:

Syntax

The basic syntax of the redis Scan command is as follows:



SCAN cursor [MATCH pattern] [COUNT count]

Available Version

= 2.8.0

Return Value

An array list.

Example

Iterate using the SCAN command:



redis 127.0.0.1:6379> scan 0   # 使用 0 作为游标,开始新的迭代

1) "17"                        # 第一次迭代时返回的游标

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  # 使用的是第一次迭代时返回的游标 17 开始新的迭代

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"

Redis Key