redis 使用scan 批量删除指定前缀的key
redis 使用scan 批量删除指定前缀的key
·
1、插入测试数据
--测试插入数据
[root@test01 ~]# vim /home/redis_insert_date.py
#!/usr/local/bin/python3
# coding=utf-8
import redis
import sys
import datetime
def create_testdata():
r = redis.StrictRedis(host='172.31.0.xx', port=6379, password='xxx', db=0)
counter = 0
with r.pipeline(transaction=False) as p:
for i in range(0, 1000):
p.set('key:' + str(i), "value" + str(i))
counter = counter + 1
if (counter == 100):
p.execute()
counter = 0
print("set by pipline loop")
if __name__ == "__main__":
create_testdata()
[root@test01 ~]#
[root@test01 ~]# chmod +x /home/redis_insert_date.py
[root@test01 ~]# /home/redis_insert_date.py
2、使用scan命令批量删除指定前缀的数据
--删除指定前缀的key 并且每次删除10个,但是不能控制删除的行数,直到删除掉所有满足条件的所有key
[root@LG-wiki-96 ~]# redis-cli -h 172.31.0.xx -p 6379 -a xxx -n 1 --scan --pattern "key:*" | xargs -L 10 redis-cli -h 172.31.0.xx -p 6379 -a xxx -n 0 del
Warning: Using a password with '-a' option on the command line interface may not be safe.
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 1
更多推荐
已为社区贡献4条内容
所有评论(0)