-
-
Save rpond-pa/18f0fbf7381b950aa5c3 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
#!/usr/bin/env ruby | |
require 'redis' | |
# Clear a large number of keys matching a pattern using SCAN and | |
# pipelining to avoid killing the server. | |
class ClearKeys | |
def initialize(pattern, host='localhost', postprocess_pattern=nil) | |
@redis = Redis.new(host: host) | |
@pattern = pattern | |
@postprocess_pattern = postprocess_pattern | |
end | |
def call | |
puts db_info | |
puts "Clearing #{@pattern}" | |
keys, time = timed { get_keys } | |
puts "#{keys.size} matching keys found in #{time} sec." | |
keys = postprocess(keys) | |
puts "#{keys.size} matching keys after postprocessing" | |
unless ENV['DRY_RUN'] | |
puts "deleting..." | |
_, time = timed { clear(keys) } | |
puts "Deleted in #{time} sec." | |
puts db_info | |
end | |
end | |
def clear(keys) | |
keys.each_slice(500) do |chunk| | |
@redis.pipelined do | |
chunk.each do |key| | |
@redis.del key | |
end | |
end | |
end | |
end | |
def postprocess(keys) | |
return keys if !@postprocess_pattern | |
regex = Regexp.new(@postprocess_pattern) | |
keys.select{ |k| regex.match k } | |
end | |
def db_info | |
"DB stats: #{@redis.info['db0']}" | |
end | |
def timed(&block) | |
start = Time.now | |
result = block.call | |
elapsed = (Time.now - start).round(2) | |
return [result, elapsed] | |
end | |
def get_keys | |
@redis.scan_each(match: @pattern, count:1000).to_a | |
end | |
end | |
if ARGV[0].nil? | |
puts "Syntax: #{$0} REDIS_PATTERN [HOST] [REGEX]" | |
else | |
ClearKeys.new(ARGV[0], ARGV[1], ARGV[2]).call | |
end |
Author
rpond-pa
commented
Oct 10, 2014
Updating the test for the proper cache clear run. Removed the ':\d+%
check. Also added noglob
to run in a z-shell.
At production, we will replace localhost
with the hostname for our cache server.
DRY_RUN=true noglob ./clear_redis_script.rb *:ovc:* localhost
DRY_RUN=true noglob ./clear_redis_script.rb *:src2:* localhost
noglob ./clear_redis_script.rb *:ovc:* localhost
noglob ./clear_redis_script.rb *:src2:* localhost
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment