-
-
Save zh/1210960 to your computer and use it in GitHub Desktop.
Patch redis and em-redis to work together
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
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