This article brings you relevant knowledge about Redis, which mainly introduces the relevant content about traversal keys and database management. Let’s take a look at it together. I hope it will be helpful to everyone.
Recommended learning: Redis video tutorial
##1.1 Full volume Traversing keys
Sometimes we need to fully traverse all keys, then we need to use the keys pattern command, and this command supports pattern matching127.0.0.1:6379> mset name luke neme josh OK
127.0.0.1:6379> keys * 1) "name" 2) "neme"
127.0.0.1:6379> keys n[a,e]me 1) "name" 2) "neme"
127.0.0.1:6379> keys n?me 1) "name" 2) "neme"
1.2 Progressive traversal
Progressive traversal is to traverse part of the key each time, then return, and continue to traverse the subsequent data the next time. In this way, all data can be traversed without blocking the redis service.scan cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> mset a 1 b 1 c 1 d 1 e 1 f 1 g 1 h 1 i 1 g 1 k 1 l 1 m 1 n 1 o 1 p 1 q 1 r 1 s 1 t 1 u 1 v 1 w 1 x 1 y 1 z 1 OK
127.0.0.1:6379> scan 0 1) "1" 2) 1) "l" 2) "f" 3) "k" 4) "y" 5) "c" 6) "e" 7) "w" 8) "d" 9) "b" 10) "o" 11) "q"
127.0.0.1:6379> scan 1 1) "23" 2) 1) "v" 2) "u" 3) "z" 4) "g" 5) "n" 6) "s" 7) "i" 8) "a" 9) "r" 10) "t"
127.0.0.1:6379> scan 23 1) "0" 2) 1) "x" 2) "h" 3) "m" 4) "p"
2.1 Switch database, select
select dbIndexdbIndex is the corresponding database serial number , there are 16 databases in the default redis configuration, and you can switch to the database number you select. For example, set a key in the default database No. 0127.0.0.1:6379> set name luke OK
127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> get name (nil)
2.2 flushall/flushdb
The difference between flushall and flushdb is that flushall will clear all data in all libraries, while flushdb will only clear the current database. This is easy to understand, so we won’t give examples. However, it should be noted that these two commands will clear all data, and the consequences of misoperation will be disastrous. And when there are too many keys, redis will also be blocked, so you must be careful when using these two commands. Recommended learning:The above is the detailed content of Detailed explanation of Redis key traversal and database management. For more information, please follow other related articles on the PHP Chinese website!