Skip to content

Instantly share code, notes, and snippets.

@zh
Forked from gregorymostizky/redis-em-patch
Created September 12, 2011 10:07
Show Gist options
  • Save zh/1210960 to your computer and use it in GitHub Desktop.
Save zh/1210960 to your computer and use it in GitHub Desktop.
Patch redis and em-redis to work together
require 'em-redis'
require 'redis'
require 'redis/distributed'
require "fiber_pool"
class Redis
class Distributed
def initialize(urls, options = {})
@tag = options.delete(:tag) || /^\{(.+?)\}/
@default_options = options
@ring = HashRing.new urls.map { |url| host, port = url_to_hostport(url); EM::Protocols::Redis.connect(host, port) }
@subscribed_node = nil
end
def url_to_hostport(url)
host, port = url.gsub(/redis:\/\//, '').split(/:/)
[host, port.to_i]
end
end
end
module EventMachine
module Protocols
module Redis
def id
"redis://#{@host}:#{@port}/#{@db}"
end
def method_missing(*argv, &blk)
f = Fiber.current
call_command(argv) do |r|
f.resume(r)
end
Fiber.yield
end
end
end
end
def create_redis_client
Redis::Distributed.new ['redis://redis602:6379', 'redis://redis602:6380', 'redis://redis612:6379', 'redis://redis612:6380']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment