程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Redis traversal and fuzzy key matching commands keys and scan (Python uses redis)

編輯:Python

Redis Traverse 、 Fuzzy matching key Two orders of keys、scan(python Use Redis)

(1) Full ergodic –keys command

obtain Redis All the keys in :

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 as a list 
print(type(keys))
print(keys)

keys Support pattern matching :

# keys Support pattern matching 
keys = redis_connection.keys("AWS_Landsat_010*") # Get to AWS_Landsat_010 For all prefixes key key 
print(keys)

pattern Detailed description of wildcards :

- * Represents matching any character
- ? To match a character
- [] Represents matching partial characters , for example [1,3] On behalf of the match 1 and 3, and [1-10] On behalf of the match 1 To 10 Any number of .
- x Transfer character , For example, to match the asterisk , Question marks need escaped characters

Tips :

stay Redis Is a single architecture , So in the implementation keys Command sometimes results in Blocking , It will block redis Multiplex io The main thread , If this thread is blocked , Between this execution, other messages are sent to redis Server commands , It's all clogging up , This leads to a series of cascade reactions , Cause instant response to Caton , Thus causing problems such as timeout , So we're using keys Command with caution . Let's take a look , Use keys Notice of order .

  • because keys Commands are blocked , So we're using keys On command , To do this in a non business client , Even though keys Orders will block , It will not affect the relevant business . It is forbidden to use in the production environment keys.
  • Such as Redis When the total number of keys in is small , You can use it directly keys command .
  • If Redis There are more keys in the , And we have to get all the keys in the client of the business environment, such as the client of the production environment , Then we can use scan command , Because this command will not block the client .

2) Progressive traversal –scan command

If you need to find and delete key The needs of , Then in the production environment, we should use scan command , Instead of keys command .

scan Advantages of command : The same is O(N) Complexity scan command , Support wildcard search ,scan Commands or other scan Such as SSCAN ,HSCAN,ZSCAN command , You don't have to block the main thread , It also supports the cursor to return data iteratively by batch , So it's an ideal choice .

keys comparison scan The advantage of the command is ,keys This is a return , and scan It requires multiple iterations to return .

scan Disadvantages of command : The returned data may be duplicated , We need to redo as needed at the business level ,scan Cursor of command from 0 Start , Also from the 0 end , Data returned each time , Will return the value that should be passed by the next cursor , We use this value , Go on to the next visit , If the data returned is empty , It does not mean that there is no data , Only the value returned by the cursor is 0 The case of represents the end .

Find all keys key:

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

Find the key that matches the pattern key:

keys2=redis_connection.scan_iter("AWS_Landsat*")
print(type(keys2))# It returns an iterator 
for key in keys2:
print(key)

Find all the elements value:
scan(cursor=0, match=None, count=None)

data=redis_connection.zscan("AWS_Landsat_01010110001010101010011010100110")# in the light of 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 – iterator
scan_iter(match=None, count=None)

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

Reference resources :

obtain Redis All the keys in - cloud + Community - Tencent cloud (tencent.com)

Use python To operate redis Usage details _Bingo-CSDN Blog


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved