Close

redis批量修改过期时间

利用python多线程以及文件读行高效率实现redis过期时间批量快速修改。

  • 更新300万数据预计耗时在20分钟。
  • 1.在命令行中键入导出所有keys
redis-cli -h 127.0.0.1 -a password -n 0 keys "*" > keys.txt
  • 2.编写python脚本
import redis
from concurrent.futures import ThreadPoolExecutor

pool = redis.ConnectionPool(host="127.0.0.1 ", password="password",
                            port=6379, db=0)
r = redis.StrictRedis(connection_pool=pool)
executor = ThreadPoolExecutor(max_workers=100)


def change_exp(line):
    line = line.strip('\n')
    r.expire(line, 2592000)
    return line


result = []
count = 0
with open("keys.txt") as f:
    tasks = [executor.submit(change_exp, line) for line in f.readlines()]
    for task in tasks:
        if task.done():
            result.append(task.result())
  • 3.执行脚本
python ./test.py

Leave a Reply

Your email address will not be published. Required fields are marked *