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 commands have SCAN, SSCAN HSCAN, ZSCAN.
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.
Advantages:
Disadvantages:
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 scan_redis.py apus* 100
__author__ = "lcl"
import sys
import redis
import os
pool=redis.ConnectionPool(host='192.168.225.128',port=6379,db=0)
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"))
f.write(key+"\n")
total = total+1
f.close
print " matching : %s As the number of :%d " % (match,total)
conclusion