Last active
September 25, 2016 21:39
-
-
Save szajbus/72f69b2f7ae94e3fc2eb to your computer and use it in GitHub Desktop.
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
# Usage: | |
# | |
# redis = Redis.new(url: ENV["REDIS_URL"]) | |
# keys = redis.scan_each(match: 'foo*', count: 1_000) | |
# keys.extend(EachWithProgress) | |
# | |
# keys.each_with_progress do |key| | |
# redis.del(key) | |
# end | |
module EachWithProgress | |
def each_with_progress | |
status = '' | |
each_with_index do |item, index| | |
$stderr.print "\b" * status.length | |
status = [index+1, size].compact.join('/') | |
$stderr.print status | |
yield(item) | |
end | |
$stderr.puts | |
end | |
end | |
def del_keys(count, pattern) | |
redis = Redis.new(url: ENV["REDIS_URL"]) | |
keys = redis.scan_each(match: pattern, count: 1_000) | |
keys.extend(EachWithProgress) | |
keys.each_with_progress do |key| | |
redis.del(key) | |
break unless (count -= 1) > 0 | |
end | |
end |
Author
szajbus
commented
Apr 13, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment