Skip to content

Instantly share code, notes, and snippets.

@danwald
Last active August 27, 2018 07:57
Show Gist options
  • Save danwald/90208e21945c5abba4c7f09f60c9f0b1 to your computer and use it in GitHub Desktop.
Save danwald/90208e21945c5abba4c7f09f60c9f0b1 to your computer and use it in GitHub Desktop.
scans redis to get key counts
import redis
import sys
ip = '172.35.5.201'
port = 6379
db = 1
sixty_days_back_sec = 5184000
thirty_days_back_sec = sixty_days_back_sec / 2
ten_days_back_sec = thirty_days_back_sec / 3
total=ten_db=thirty_db=sixty_db=deleted=0
print_every = 1000
delete_every = 100
delete = True
r = redis.StrictRedis(host=ip, port=port, db=db)
pipe = r.pipeline()
for key in r.scan_iter():
age = r.object('idletime', key)
if age > sixty_days_back_sec:
sixty_db += 1
if delete:
pipe.delete(key)
if age > thirty_days_back_sec:
thirty_db += 1
if age > ten_days_back_sec:
ten_db += 1
total += 1
if not total % print_every:
sys.stdout.write("\r{} total keys, {:06.3f}% older than 60 days, {:06.3f}% older than 30 days, {:06.3f}% older 10 days, {:06.3f}% less than 10. Deleted {} keys".format(
total,
float(sixty_db)/float(total)*100.0,
float(thirty_db)/float(total)*100.0,
float(ten_db)/float(total)*100.0,
float(total-ten_db)/float(total)*100.0,
deleted
)
)
sys.stdout.flush()
if delete and not total % delete_every:
deleted += len(filter(None, pipe.execute()))
print "{} total keys, {} keys older than 60 days, {} keys older than 30 days, {} older keys 10 days, {} keys less that 10 days. Deleted {} keys".format(total, sixty_db, thirty_db, ten_db, total-ten_db, deleted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment