Created
June 16, 2015 23:37
-
-
Save tysonmote/1fa32995b25d49baddac to your computer and use it in GitHub Desktop.
Delete large Redis sorted sets 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 SafeSortedSetDelete | |
@queue = :garbage_collection | |
BATCH_SIZE = 100 | |
# Rename the key and queue for deletion | |
def self.delete(key) | |
newkey = "gc:zsets:#{$redis.incr("gc:index")}" | |
$redis.rename(key, newkey) | |
Resque.enqueue(self, newkey) | |
end | |
# Delete members from the sorted set in batches | |
def self.perform(key) | |
while $redis.zcard(key) > 0 | |
$redis.zremrangebyrank(key, 0, BATCH_SIZE - 1) | |
end | |
end | |
end | |
# Example: | |
# | |
# SafeSortedSetDelete.delete("my.large.zset") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment