Created
June 14, 2019 02:02
-
-
Save clintmod/2d22e749e43c42bb234474010b14a6e9 to your computer and use it in GitHub Desktop.
Removes keys older than X days
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'redis' | |
pattern = ARGV[1] || "*" | |
days = ARGV[0].to_i > 0 ? ARGV[0].to_i : 30 | |
MAX_AGE = days * 60 * 60 * 24 # convert to seconds | |
redis = Redis.new | |
cursor = 0 | |
loop do | |
cursor, keys = redis.scan cursor, :match => pattern | |
keys.each do |key| | |
idle_time = redis.object('idletime', "#{key}").to_i | |
if idle_time > MAX_AGE | |
puts "Deleting key #{key} with idle time of #{idle_time} which is older than #{MAX_AGE}" | |
redis.unlink key | |
end | |
end | |
break if cursor == '0' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment