Redis键管理

xiaohai 2021-05-28 21:05:06 1768人围观 标签: Redis 
简介首先我们通过fake2db生成一批测试数据,生成过程查看《Python常用包整理-fake2db:一个生成测试数据的工具》

首先我们通过fake2db生成一批测试数据,生成过程查看《Python常用包整理-fake2db:一个生成测试数据的工具》

1、数据准备

#进入redis-cli刷新db
127.0.0.1:6379> FLUSHALL
OK

#通过fake2db写入测试数据
[root@localhost ~]# fake2db --rows 10000 --db redis
2018-07-06 23:14:13,894 root     Rows argument : 10000
2018-07-06 23:14:32,266 root     simple_registration Commits are successful after write job!
2018-07-06 23:15:27,276 root     detailed_registration Commits are successful after write job!
2018-07-06 23:16:55,702 root     companies Commits are successful after write job!
2018-07-06 23:16:58,608 root     user_agent Commits are successful after write job!
2018-07-06 23:18:06,923 root     customer Commits are successful after write job!

2、通过DBSIZE获取键的个数

127.0.0.1:6379> DBSIZE
(integer) 50000

3、通过KEYS命令获取所有的键

127.0.0.1:6379> KEYS * 
...
49994) "customer:3129"
49995) "company:2424"
49996) "simple_registration:3893"
49997) "company:308"
49998) "user_agent:3968"
49999) "detailed_registration:1190"
50000) "user_agent:8304"
(1.33s)

4、通过SCAN迭代获取所有的键

127.0.0.1:6379> SCAN 0
1) "4096"
2)  1) "customer:9271"
    2) "customer:3949"
    3) "company:6902"
    4) "simple_registration:8251"
    5) "user_agent:5049"
    6) "customer:2693"
    7) "detailed_registration:13"
    8) "customer:348"
    9) "company:7015"
   10) "customer:2842"

127.0.0.1:6379> SCAN 4096 #从上一次迭代的游标返回值继续
1) "34816"
2)  1) "simple_registration:4892"
    2) "customer:7291"
    3) "customer:6890"
    4) "detailed_registration:7336"
    5) "user_agent:3771"
    6) "company:3778"
    7) "detailed_registration:2386"
    8) "simple_registration:7600"
    9) "detailed_registration:5166"
   10) "simple_registration:2352"


127.0.0.1:6379> SCAN 0 count 20 #指定返回的个数,默认10个
1) "34816"
2)  1) "customer:9271"
    2) "customer:3949"
    3) "company:6902"
    4) "simple_registration:8251"
    5) "user_agent:5049"
    6) "customer:2693"
    7) "detailed_registration:13"
    8) "customer:348"
    9) "company:7015"
   10) "customer:2842"
   11) "simple_registration:4892"
   12) "customer:7291"
   13) "customer:6890"
   14) "detailed_registration:7336"
   15) "user_agent:3771"
   16) "company:3778"
   17) "detailed_registration:2386"
   18) "simple_registration:7600"
   19) "detailed_registration:5166"
   20) "simple_registration:2352"

5、使用DEL命令或UNLINK命令删除键,UNLINK命令在Redis4.0以上版本引入,主要用于执行大KEY的异步删除

127.0.0.1:6379> DEL "customer:9271" "customer:3949"
(integer) 2
127.0.0.1:6379> DBSIZE
(integer) 49998
127.0.0.1:6379> UNLINK "company:6902"
(integer) 1
127.0.0.1:6379> DBSIZE
(integer) 49997

6、使用EXISTS命令判断一个键是否存在

127.0.0.1:6379> EXISTS "company:6925"
(integer) 1

127.0.0.1:6379> EXISTS "company:692500000"
(integer) 0

7、使用TYPE命令获取键的数据类型

127.0.0.1:6379> TYPE "company:6925"
hash

8、使用RENAME命令来重命名一个键

127.0.0.1:6379> RENAME "company:6925" "aaa_999"
OK
127.0.0.1:6379> EXISTS "company:6925"
(integer) 0
127.0.0.1:6379> EXISTS "aaa_999"
(integer) 1
注意:

1、KEYS命令尽量不要在键太多的场景下使用,而使用SCAN命令代替,否则会使得服务器阻塞
2、DEL命令也是,如果删除的键是字符串以外的数据类型,那么当元素的数据量很大时可能会使服务器延迟。所以最好使用UNLINK代替。UNLINK会在另一个线程而不是主事件循环线程中执行删除操作,不会阻塞事件的处理。
3、RENAME命令会在目标键已存在时将其删除,如DEL可能导致高延迟,所以先将目标键使用UNLINK删除后再重名了。

其他命令

DUMP/RESTORE命令可以用于序列化和反序列化操作,所以可以使用这两个命令对Redis进行备份和恢复。