python3使用redis库批量删除未设置过期时间的key

2019-12-02  本文已影响0人  小黑佬

python使用redis库批量删除未设置过期时间的key

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: chunk 
# Date:2019-11-18
# Desctiption: redis 

import redis
import pprint
import time


def del_redis_ex(dbpasswd=None):
    # r = redis.Redis(host='127.0.0.1',port=6379,db=0)
    pool = redis.ConnectionPool(host='127.0.0.1',port=7000,db=0,password=dbpasswd)
    r = redis.StrictRedis(connection_pool=pool,decode_responses=True)
    print(type(r))


    keys = r.keys()
    for key in keys:
            time.sleep(0.5)
            key = key.decode()
            print(type(r.pttl(key)))
            print(r.pttl(key))
            if r.pttl(key) == -1:
                r.delete(key)
                print(" 删除的key是 {0}".format(key))
                
            if key == 'runoobkey':
                print(r.get(key))


del_redis_ex(dbpasswd='123456')

上一篇下一篇

猜你喜欢

热点阅读