Created
August 24, 2017 02:56
-
-
Save emperorcezar/071ea448a07e27e46e195dc2b92fb0c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import redis | |
parser = argparse.ArgumentParser(description='Get sizes using key pattern.') | |
parser.add_argument('pattern', metavar='PATTERN', nargs=1, | |
help='pattern to use') | |
parser.add_argument('--host', dest='host', default='localhost', | |
help='redis host') | |
parser.add_argument('--port', dest='port', default=6379, | |
help='redis port') | |
parser.add_argument('--db', dest='db', default=0, | |
help='redis db') | |
args = parser.parse_args() | |
def main(): | |
host = args.host | |
port = args.port | |
db = args.db | |
cli = redis.Redis(host=host, port=port, db=db) | |
pattern = args.pattern[0] | |
for key in cli.scan_iter(pattern): | |
key_type = cli.type(key) | |
if key_type == 'string': | |
size = cli.strlen(key) | |
if key_type == 'list': | |
size = cli.llen(key) | |
if key_type == 'set': | |
size = cli.scard(key) | |
if key_type == 'zset': | |
raise NotImplementedError | |
if key_type == 'hash': | |
size = cli.hlen(key) | |
print('{}, {}, {}'.format(key_type, key, size)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment