redis批量修改過期時(shí)間

利用python多線程以及文件讀行高效率實(shí)現(xiàn)redis過期時(shí)間批量快速修改。

  • 更新300萬數(shù)據(jù)預(yù)計(jì)耗時(shí)在20分鐘。
  • 1.在命令行中鍵入導(dǎo)出所有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.執(zhí)行腳本
python ./test.py
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容