python脚本查询redis key的空闲时间
Posted xiewenda8
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python脚本查询redis key的空闲时间相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import redis
import time
FILE_DATA_PATH = '/Users/xiewenda/Desktop/'
def findRedisKey(dbIndex):
redis_cache = redis.StrictRedis(host="127.0.0.1", port=6379, username=None, password=None, db=dbIndex)
# pool = redis.ConnectionPool(host='192.168.3.128', port=6379, db=0)
# r = redis.StrictRedis(connection_pool=pool)
print("begin")
print(redis_cache.dbsize())
begin_pos = 0
while True:
result = redis_cache.scan(begin_pos, None, 1000)
return_pos, datalist = result
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())), "return_pos:", return_pos)
if len(datalist) > 0:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())), "datalist size:", len(datalist))
for key in datalist:
#if redis_cache.ttl(key) > 60 * 24 * 30 | redis_cache.ttl(key) == -1: #剩余过期期间或者没设置过期时间
if redis_cache.object('idletime',key) > (60 * 60 * 24 * 10): # 空闲时间大于10天
yield key
if return_pos == 0:
break
begin_pos = return_pos
print("game over")
if __name__ == '__main__':
dbIndexs = range(16)
fileName = FILE_DATA_PATH+'redisKey.txt'
f = open(fileName, 'w')
for dbIndex in dbIndexs:
f.write('****************************** db'+str(dbIndex)+' *****************************\\n')
f.writelines(map(lambda key: key.decode('utf-8', errors='ignore')+"\\n", findRedisKey(dbIndex)))
f.write('\\n')
f.close()
以上是关于python脚本查询redis key的空闲时间的主要内容,如果未能解决你的问题,请参考以下文章