背景
有些业务需要删除一些不知来源的key,由于不确定是否会影响线上,所以需要在删除前先备份
于是写了个备份str类型的key的小工具方便业务方参考
注意:一次操作的key的个数不要超过1000个,否则会影响proxy的性能
#!/bin/python3 import redis keys_f="keys.txt" set_cmd_f="mset.txt" n=0 para=1000 keys_roll=[] proxy_ip='127.0.0.1' port=9999 def getSetCmd(keys): set_cmd=["mset"] r = redis.StrictRedis(host=proxy_ip, port=port, db=0, socket_timeout=0.5, socket_connect_timeout=0.5) values=r.mget(keys) for i in range(len(values)): k=keys_roll[i] value=values[i] # key不存则不考虑 if value is not None: v=str(value.decode('utf-8')) if v=='': v='""' set_cmd.append(k) set_cmd.append(v) set_cmd=' '.join(set_cmd) print(set_cmd) with open(set_cmd_f,'a') as inf: inf.write(set_cmd+' ') with open(keys_f) as out: keys=out.readlines() for key in keys: key=key.strip() # strip " " n=n+1 mo=n%para keys_roll.append(key) if mo==0: getSetCmd(keys_roll) keys_roll=[] # 最后一批次不满设置的并发度 if mo != 0: getSetCmd(keys_roll) keys_roll=[]