当前位置:网站首页>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;
}
}
边栏推荐
- Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
- RGB与CMYK颜色模式
- [learning resources] understand and love mathematics
- Redis学习笔记—Redis与Lua
- [event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
- An idea of using keep alive to cache data in vue3 form pages
- Redis学习笔记—地理信息定位(GEO)
- JS mask important data of ID card and mobile phone number with * *
- [QNX Hypervisor 2.2用户手册]6.2 网络
- Redis学习笔记—数据类型:哈希(hash)
猜你喜欢
随机推荐
Unique paths for leetcode topic resolution
Unity grid programming 08
Kotlin Series 1: getting started with basics
Simple student management
636. Exclusive Time of Functions
JSP入门总结
Node request module cookie usage
自定义标签——jsp标签基础
Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis
The results of CDN node and source station are inconsistent
S5P4418裸机编程的实现(替换2ndboot)
在小程序中实现视频通话及互动直播的一种方法
@Response
“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册
Structure binary tree from inorder and postorder traversal for leetcode topic analysis
Lua的基本使用
670. Maximum Swap
297. Serialize and Deserialize Binary Tree
MQTT+Flink实现实时消息的订阅与发布
文件的打开新建与存储


![[cloud native | kubernetes] kubernetes principle and installation (II)](/img/db/dd93bbcac6d0404d44f67d2da12880.png)






