当前位置:网站首页>Redis learning notes - traverse key

Redis learning notes - traverse key

2022-06-23 09:10:00 Love Guoba

Full traversal key

keys pattern

View all key、“J” At the beginning key、“T” perhaps “J” At the beginning key

127.0.0.1:6379> keys *
1) "Tom"
2) "Jerry"
3) "hello"
4) "Java"
127.0.0.1:6379> keys J*
1) "Jerry"
2) "Java"
127.0.0.1:6379> keys [T,J]*
1) "Tom"
2) "Jerry"
3) "Java"

If redis Data volume data volume has a large number of keys , keys Command may cause redis Blocking , Avoid using in production if you don't know how many keys there are keys command

Progressive traversal

Redis from 2.8 After version , Provides a new command scan, It can effectively solve keys The problem with the command . and keys When the command is executed, it traverses all the different keys ,scan Adopt the way of gradual traversal to solve keys The possible blocking problem of command , Every time scan The time complexity of the command is O(1), But to really achieve keys The function of , It needs to be done many times scan.

Redis Storing key values for the actual use of is hashtable Data structure of , So every time you execute scan, Think of it as scanning only a few keys in a dictionary , Until all the keys in the dictionary are traversed

scan cursor [MATCH pattern] [COUNT count]
  • cursor Is the required parameter , actually cursor It's a cursor , The first traversal from 0 Start , Every time scan Returns the value of the current cursor after the traversal , Until the value of the bid is 0, End of traversal
  • match pattern Is an optional parameter , What it does is it matches patterns , This and keys Pattern matching is similar
  • count number Is an optional parameter , What it does is it says the number of keys to traverse at a time , The default value is 10, This parameter can be appropriately increased

Put just 6666 The port instance is cleared (flushdb command ), Then add English letters 26 individual key Used for testing

127.0.0.1:6666> flushdb
OK
127.0.0.1:6666> keys *
(empty list or set)
127.0.0.1:6666> mset a a b b c c d d e e f f g g h h i i j j k k l l m m n n o o p p q q r r s s t t u u v v w w x x y y z z 
OK

First execution scan0, The return result is divided into two parts : The first part 1 Next time scan Needed cursor, The second part is 10 Key :

127.0.0.1:6666> scan 0
1) "1"
2)  1) "u"
    2) "w"
    3) "g"
    4) "a"
    5) "b"
    6) "m"
    7) "z"
    8) "q"
    9) "i"
   10) "y"

Use the new cursor=“1”,scan 1 Return to the next cursor=“29” It's a key

127.0.0.1:6666> scan 1
1) "29"
2)  1) "n"
    2) "e"
    3) "t"
    4) "f"
    5) "c"
    6) "s"
    7) "h"
    8) "x"
    9) "o"
   10) "j"

Use the new cursor=“29”,scan 29 Return results cursor become 0, All keys have been traversed

127.0.0.1:6666> scan 29
1) "0"
2) 1) "p"
   2) "v"
   3) "r"
   4) "l"
   5) "k"
   6) "d"

except scan outside ,Redis Provides hash oriented types 、 Collection types 、 Scan traversal command for ordered sets , Solve problems such as hgetall、smembers、zrange Possible blocking problems , The corresponding commands are hscan、sscan、zscan, Their usage and scan similar , Let's say sscan Explain for example , There are two types of elements in the current collection , For example, use old:user and new:user start , First, you need to put old:user Delete all the elements at the beginning , You can refer to the following pseudo code :

String key = "myset";
// Definition  pattern
String pattern = "old:user*";
// The cursor starts from each time  0  Start 
String cursor = "0";
while (true) {
    
    // Get scan results 
    ScanResult scanResult = redis.sscan(key, cursor, pattern);
    List elements = scanResult.getResult();
    if (elements != null && elements.size() > 0) {
    
    // Batch deletion 
        redis.srem(key, elements);
    }
    // Get new cursor 
    cursor = scanResult.getStringCursor();
    // If the cursor is  0  End of traversal 
    if ("0".equals(cursor)) {
    
        break;
    }
}
原网站

版权声明
本文为[Love Guoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230900185314.html