Redis traverses and vaguely matches the two commands key s and scan (python uses redis)

Redis traverses and vaguely matches the two commands key s and scan (python uses redis)

(1) Full traversal – keys command

Get all keys in Redis:

import redis  
  
pool=redis.ConnectionPool(host='10.3.1.151',port=6379,password='mca321',db=2)  
r = redis.tRedis(connection_pool=pool)  

# Get change = all keys under the database
keys = r.keys()  
# Get the data structure type of the key --- get all the keys in the form of a list
print(type(keys))  
print(keys)

keys supports pattern matching:

# keys supports pattern matching
keys = redis_connection.keys("AWS_Landsat_010*") #Get AWS_ Landsat_ All key keys prefixed with 010
print(keys)

Detailed description of pattern wildcard:

- * Represents matching any character
- ? Represents matching a character
- [] Represents the matching part of the character, for example[1,3]Represents matches 1 and 3, and[1-10]Represents any number that matches 1 to 10.
- x Transfer characters, such as characters that need to be escaped to match asterisks and question marks

Tips:

In redis, there is a single architecture, so sometimes blocking occurs when executing the keys command, which will block the io main thread of redis multiplexing. If this thread is blocked, other commands sent to the redis server during execution will be blocked, resulting in a series of cascade reactions, resulting in instantaneous response jamming, resulting in timeout and other problems, So we should use the keys command with caution. Let's take a look at the precautions for using the keys command.

  • Because the keys command is blocked, we should use the keys command in the non business client, so that even if the keys command is blocked, it will not affect the relevant business. Keys are not allowed in the production environment.
  • For example, if the total number of keys in Redis is relatively small, you can directly use the keys command.
  • If the total number of keys in Redis is relatively large, and we have to obtain all keys in the business environment client, such as the production environment client, we can use the scan command, because this command will not block the client.

2) Progressive traversal – scan command

If we need to find and delete the key requirement, we should use the scan command instead of the keys command in the production environment

Advantages of scan command: it is also a scan command with O (N) complexity. It supports generic search. Scan command or other scan commands such as SSCAN, HSCAN and ZSCAN commands can not block the main thread and support the cursor to return data by batch iteration, so it is an ideal choice.

Compared with the scan command, keys has the advantage that keys is returned at one time, while scan requires multiple iterations.

Disadvantages of scan command: the returned data may be repeated, so we need to redo it as needed in the business layer. The cursor of scan command starts from 0 and ends from 0. Each time the returned data will return the value that should be transmitted by the next cursor. We will access it again according to this value. If the returned data is empty, it does not mean that there is no data, Only when the value returned by the cursor is 0 represents the end.

Find all key s:

keys3=redis_connection.scan_iter()
print(type(keys3))# An iterator is returned
for key in keys3:
    print(key)
    
keys3=redis_connection.scan()
print(type(keys3))# A tuple is returned

Find the key matching the pattern:

keys2=redis_connection.scan_iter("AWS_Landsat*")
print(type(keys2))# An iterator is returned
for key in keys2:
    print(key)

Find all element value s:
scan(cursor=0, match=None, count=None)

data=redis_connection.zscan("AWS_Landsat_01010110001010101010011010100110")#For zset data structure
print(type(data))
print(data)


# Other data structures:
print(r.hscan("hash2"))
print(r.sscan("set3"))
print(r.zscan("zset2"))
print(r.getrange("foo1", 0, -1))
print(r.lrange("list2", 0, -1))
print(r.smembers("set3"))
print(r.zrange("zset3", 0, -1))
print(r.hgetall("hash1"))12345678

View all elements – iterators
scan_iter(match=None, count=None)

data=redis_connection.zscan_iter("AWS_Landsat_01010110001010101010011010100110")#For zset data structure
print(type(data))
print(data)
for d in data:
    print(d)

reference resources:

Get all keys in Redis - cloud + community - Tencent cloud (tencent.com)

How to use python to operate redis_ Bingo CSDN blog

Keywords: Python Database Redis

Added by dirkdetken on Wed, 16 Feb 2022 10:03:00 +0200