zoukankan      html  css  js  c++  java
  • redis查找大key

    redis中查找出比较大的key 

    下面直接上代码 (请在测试机上测试)


    #!/usr/bin/env python import sys import redis def check_big_key(r, k): bigKey = False length = 0 try: type = r.type(k) if type == "string": length = r.strlen(k) elif type == "hash": length = r.hlen(k) elif type == "list": length = r.llen(k) elif type == "set": length = r.scard(k) elif type == "zset": length = r.zcard(k) except: return if length > 10240: bigKey = True if bigKey: print db, k, type, length def find_big_key_normal(db_host, db_port, db_num): db_password = "123456" r = redis.StrictRedis(host=db_host, port=db_port, db=db_num,password=db_password) for k in r.scan_iter(count=1000): check_big_key(r, k) def find_big_key_sharding(db_host, db_port, db_num, nodecount): r = redis.StrictRedis(host=db_host, port=db_port, db=db_num) cursor = 0 for node in range(0, nodecount): while True: iscan = r.execute_command("iscan", str(node), str(cursor), "count", "1000") for k in iscan[1]: check_big_key(r, k) cursor = iscan[0] print cursor, db, node, len(iscan[1]) if cursor == "0": break; if __name__ == '__main__': if len(sys.argv) != 3: print 'Usage: python ', sys.argv[0], ' host port password ' exit(1) db_host = sys.argv[1] db_port = sys.argv[2] db_password = "123456" r = redis.StrictRedis(host=db_host, port=int(db_port),password=db_password) nodecount = 1 #nodecount = r.info()['nodecount'] keyspace_info = r.info("keyspace") for db in keyspace_info: print 'check ', db, ' ', keyspace_info[db] if nodecount > 1: find_big_key_sharding(db_host, db_port, db.replace("db", ""), nodecount) else: find_big_key_normal(db_host, db_port, db.replace("db", ""))

      

  • 相关阅读:
    xftp,winscp显示隐藏文件
    今日校园-打卡
    dungeon quest(又名暗黑遗迹,勇闯地下城等)装备体系简述
    frp转发
    虚拟机win10添加新磁盘
    JEECG(一) 如何配置自己的业务包
    C# 获取Url 请求方式 域名 端口 路径
    js url 参数 转换成 json 对象数据
    VisualStudio2017 远程 调试 IIS 服务器 web网站
    c# MVC Action 如何知道 发送方给你的 Json 数据的格式内容是什么
  • 原文地址:https://www.cnblogs.com/soilge/p/11641327.html
Copyright © 2011-2022 走看看