根据指定规则删除永不过期的redisKey

python 使用redis-scan删除永不过期的redisKey

# -*- coding:utf-8 -*-
# scan匹配指定规则key并删除永无过期时间的key

import redis

# 匹配test:开头[0-9]结尾的key
prefix = 'test:*[0-9]'
conn = redis.Redis(host = '127.0.0.1', port=6379, password="123456")
i = 0
s = 0
for key in conn.scan_iter(match=prefix, count=1000):
    ttltime = conn.ttl(key)
    i = i + 1
    if(ttltime is None or ttltime == -1):
        # 删除匹配到的没有过期时间的key
        conn.delete(key)
        s = s + 1
        print(key, ' - del number=', s)
        

print('total key', i,' -> del ',s)
posted @ 2021-09-10 18:04  JaminX86  阅读(247)  评论(0编辑  收藏  举报