Last active
February 13, 2017 22:49
-
-
Save tysonmote/9cda15bde7ffcb1e6b99 to your computer and use it in GitHub Desktop.
Delete large Redis lists with Ruby and Resque
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
$redis = Redis.new | |
class SafeListDelete | |
@queue = :garbage_collection | |
BATCH_SIZE = 100 | |
# Rename the key and queue for deletion | |
def self.delete(key) | |
newkey = "gc:lists:#{$redis.incr("gc:index")}" | |
$redis.rename(key, newkey) | |
Resque.enqueue(self, newkey) | |
end | |
# Trim off elements in batches | |
def self.perform(key) | |
while $redis.llen(key) > 0 | |
$redis.ltrim(key, 0, -BATCH_SIZE - 1) | |
end | |
end | |
end | |
# Example: | |
# | |
# SafeListDelete.delete("my.large.list") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment