Skip to content

Instantly share code, notes, and snippets.

@maguec
Created March 18, 2024 15:55
Show Gist options
  • Save maguec/5549dbb236ef89130803b68a707b4698 to your computer and use it in GitHub Desktop.
Save maguec/5549dbb236ef89130803b68a707b4698 to your computer and use it in GitHub Desktop.
redis_keyspace_by_app_prefix.py
#!/usr/bin/env python
## This script*will* slow down the Redis server. Run with caution!!
app_keys = {}
app_size = {}
app_ttls = {}
from redis import StrictRedis
redis = StrictRedis.from_url("redis://localhost:6379")
for key in redis.scan_iter('*', 1000):
k = key.decode('utf-8')
app = k.split(':')[0]
if app not in app_keys:
app_keys[app] = 0
app_size[app] = 0
app_ttls[app] = 0
app_keys[app] += 1
app_size[app] += redis.memory_usage(key)
if redis.ttl(key) > 0:
app_ttls[app] += 1
print("Keycount")
print(app_keys)
print("Total bytes")
print(app_size)
print("Number of keys with TTL")
print(app_ttls)
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment