清理redis的key

import redis
import time

pool=redis.ConnectionPool(host='xxxxxxx', port=6379, max_connections=100)

r = redis.StrictRedis(connection_pool=pool)


def del_keys_with_pipe():
    start_time = time.time()
    result_length = 0
    pipe = r.pipeline()
    for key in r.scan_iter(match='sociaxx*', count=2000):
        pipe.delete(key)
        result_length += 1
        if result_length % 2000 == 0:
             pipe.execute()
    ip_time = time.time()
    print("use pipeline scan time :", time.time() - start_time)
    pipe.execute()

    print ("use pipeline ways delete numbers:", result_length)

if  __name__ == '__main__':
    del_keys_with_pipe()

  

业余经济爱好者
原文地址:https://www.cnblogs.com/5sdba-notes/p/14613839.html