西安外贸网站建设公司在线域名查询网站
首先,安装 redis-py 库:
pip install redis
其次,创建一个 Python 脚本来连接到 Redis 并删除匹配的键。假设 Redis 端口是 6379,密码是 mypassword(如果有密码的话)。
import redis# 连接到 Redis
r = redis.StrictRedis(host='localhost', port=6379, db=0, password='mypassword')# 初始游标
cursor = 0
prefix = 'gaode:around:*'# 使用 SCAN 命令遍历 Redis 键
while True:cursor, keys = r.scan(cursor, match=prefix, count=1000)# 如果找到匹配的键,删除它们if keys:r.delete(*keys)# 如果游标为 0,表示扫描完成if cursor == 0:breakprint("Batch deletion completed!")
解释:
r.scan(cursor, match=prefix, count=1000):扫描 Redis 中所有匹配gaode:around:*模式的键。r.delete(*keys):删除找到的键。- 循环直到游标返回
0,表示扫描完成。
最后:运行脚本
将该 Python 脚本保存为 delete_keys.py,然后执行:
python delete_keys.py
这样,Python 脚本会连接到 Redis 容器并批量删除所有以 gaode:around: 开头的键。
请根据自己需要将要删除的键的前缀,redis host,port,auth替换为自己的配置。
