Created
January 2, 2013 18:28
-
-
Save balepc/4436680 to your computer and use it in GitHub Desktop.
Move keys from multiple sources into one. Works only with strings
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
require 'rubygems' | |
require 'redis' | |
require 'uri' | |
class Redis | |
module Helper | |
def parse_redis_url(redis_url) | |
node = URI(redis_url) | |
path = node.path | |
db = path[1..-1].to_i rescue 0 | |
{ | |
:host => node.host, | |
:port => node.port || 6379, | |
:db => db | |
} | |
end | |
end | |
end | |
class Redis | |
class Migrator | |
include Redis::Helper | |
attr_accessor :from, :to | |
def initialize(from, to) | |
self.from = from.collect{|url| Redis.new(parse_redis_url(url)) } | |
self.to = to.collect{|url| Redis.new(parse_redis_url(url)) } | |
end | |
def migrate | |
self.from.each do |from| | |
self.to.each do |to| | |
keys = from.keys('*') | |
keys.each do |key| | |
value = from.get(key) | |
ttl = from.ttl(key) | |
if ttl > 1 | |
puts "Moving key #{key}=#{value} with ttl #{ttl}" | |
to.setex(key, ttl, value) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
from = ["redis://192.168.0.69:6382/5", "redis://192.168.0.69:6383/5"] | |
to = ["redis://localhost:6379/5"] | |
@migrator = Redis::Migrator.new(from, to) | |
@migrator.migrate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment