Redis gets an instance of an key script with a prefix

  • 2020-06-03 08:44:46
  • OfStack

1. The background

In normal maintenance, it is often encountered to count the number of key with a prefix, and in redis with a high number of requests, keys * directly causes a block.
You can use scan for incremental iterations, and pipeline for queries to reduce interaction and improve efficiency.

2. Advantages and disadvantages of the scan command


SCAN is going to iterate over all keys

The other SCAN commands are the set selected by SCAN.

The SCAN command is an incremental loop that returns only a small number of elements per call. So there will be no pit of the KEYS command.

The SCAN command returns a cursor that starts at 0 and ends at 0.

scan 0
1) "655"
2) 1) "test1"
  2) "test2"

The return value is 1 array,1 cursorId for the next loop, and 1 element array. The SCAN command does not guarantee that each return will be in order, and it is possible to return multiple times with the same key, making no distinction and requiring application processing.

In addition, the SCAN command can specify COUNT, which defaults to 10. But this doesn't mean you can return as many as you specify, it's just a hint, it doesn't guarantee that 1 will return as many.


Provide key space traversal operation, support cursor, complexity O(1), whole traversal 1 only need O(N); Provide result pattern matching; Supports the number of data bars returned once, but it is only hints, which sometimes returns more; Weak state, all states only need the client to maintain 1 cursor;


Complete fast traversal cannot be provided, that is, if there is data modification in the middle, some data involving modification may not be traversed; The number of data bars returned each time is not fixed, relying heavily on internal implementation; The data returned may be duplicated and the application layer must be able to handle the reentrant logic.

3. Implementation of python script

There is one encapsulated function in python, scan_iter-- view all elements -- iterator

Script content:

#!/usr/bin/env python
# -*- coding: UTF-8 -*- 
# Effect: Counts a prefix key , and enter it into the file 
# Usage: python apus* 100
__author__ = "lcl" 
import sys
import redis 
import os 
r = redis.StrictRedis(connection_pool=pool) 
# Scan for matching values, pass sys.argv The ginseng 
match = sys.argv[1]
# Number of matches per time 
count = sys.argv[2]
#print match
#print count
# The total number 
total = 0
# Scan the key Output to file 
path = os.getcwd()
# Scan the key Output file 
txt = path+"/keys.txt"
f = open(txt,"w")
for key in r.scan_iter(match = match,count = count):
# f.write("%s %s" % (key,"\n"))
 total = total+1
print " matching : %s  As the number of :%d " % (match,total)


Related articles: