w3resource

Redis Keys: SCAN

Redis SCAN Command

The Redis SCAN command  is used in order to incrementally iterate over a collection of elements.

Basic usage of SCAN

  • SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.
  • An iteration starts when the cursor is set to 0 and terminates when the cursor returned by the server is 0. The following is an example of SCAN iteration:

Syntax:

SCAN cursor [MATCH pattern] [COUNT count]

Available since

2.8.0.

Return Value

SCAN return value is an array of two values:

  • the first value is the new cursor to use in the next call, the second value is an array of elements
  • Since in the second call the returned cursor is 0, the server signaled to the caller that the iteration finished, and the collection was completely explored.
  • Starting an iteration with a cursor value of 0, and calling SCAN until the returned cursor is 0 again is called a full iteration.

Return Value Type

Array

Example: Redis SCAN key

127.0.0.1:6379> SCAN 0
1) "1"
2)  1) "numbers"
    2) "hash key"
    3) "myhash"
    4) "myset"
    5) "w3rkey"
    6) "user-v"
    7) "languages"
    8) "mycolor1"
    9) "user-x"
   10) "key1"
   11) "key"
   12) "string key"

Available count

How is the scan cursor count 10.

The specified key and count the number of queries that do not always fit perfectly. In consideration of the processing time and controlling the number.

Example: Redis SCAN count 10

127.0.0.1:6379> SCAN 0 count 10
1) "1"
2)  1) "numbers"
    2) "hash key"
    3) "myhash"
    4) "myset"
    5) "w3rkey"
    6) "user-v"
    7) "languages"
    8) "mycolor1"
    9) "user-x"
   10) "key1"
   11) "key"
   12) "string key"

Enable pattern

The usage scan cursor match pattern.

The lookup key only meets the specified pattern.

The GLOB style pattern.

Example: Redis SCAN match key*

127.0.0.1:6379> SCAN 0 match key*
1) "1"
2) 1) "key1"
   2) "key"

Example: Redis HSCAN another example

127.0.0.1:6379> HMSET nhash name Sachin age 40
OK
127.0.0.1:6379> HSCAN nhash 0
1) "0"
2) 1) "name"
   2) "Sachin"
   3) "age"
   4) "40"

Previous: RESTORE
Next: SORT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-scan-key.php