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 OKIf you want To traverse all keys, you can use the command keys *
127.0.0.1:6379> keys * 1) "name" 2) "neme"pattern uses glob-style wildcards, where:
127.0.0.1:6379> keys n[a,e]me 1) "name" 2) "neme"You can also operate like this
127.0.0.1:6379> keys n?me 1) "name" 2) "neme"But when there are a large number of keys in the redis database, keys will block redis. What should we do if we need to traverse keys? Generally our production environment is multi-node, then we can find a redis slave node that does not provide external services to traverse the data, but if the amount of data is large, it will still block redis, but for the slave node, it is just Affected master-slave replication. If you are sure that there are not many keys on redis, you can execute it directly.
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]The parameters are explained as follows:
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 OKWe use scan to traverse, the first execution returns the following:
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"The second time uses the 1 returned by the first time When traversing, you can traverse to 10 keys
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"The third time you use the 23 returned for the second time to traverse. When the return is 0, it means that the traversal is completed
127.0.0.1:6379> scan 23 1) "0" 2) 1) "x" 2) "h" 3) "m" 4) "p"At the same time, it also There are hscan for hash types, sscan for set types, and zscan for ordered sets. The usage methods are the same as scan2 Database managementThere are several other redis A command for database operations: dbsize, select, flushdb/flushall
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 OKThen we switch to database No. 1 to obtain the key, but it cannot be obtained, indicating that there are differences between databases in a redis service Not interoperable.
127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> get name (nil)So can it be used as multiple redis? Of course not. Although there are more libraries, since redis is single-threaded, it is actually still a CPU. If a command in a database is executed very slowly, other libraries will also be affected. Therefore, in this case, if it is blocked by other libraries, For developers using a certain library, it can be difficult to analyze what the problem is.
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!