X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/ed9b544e10b84cd43348ddfab7068b610a5df1f7..c9a111acf47cb5bb2138d1f699253f87d68e53e8:/client-libraries/ruby/lib/dist_redis.rb diff --git a/client-libraries/ruby/lib/dist_redis.rb b/client-libraries/ruby/lib/dist_redis.rb index 04505c20..830f8b6d 100644 --- a/client-libraries/ruby/lib/dist_redis.rb +++ b/client-libraries/ruby/lib/dist_redis.rb @@ -2,13 +2,20 @@ require 'redis' require 'hash_ring' class DistRedis attr_reader :ring - def initialize(*servers) - srvs = [] - servers.each do |s| - server, port = s.split(':') - srvs << Redis.new(:host => server, :port => port) + def initialize(opts={}) + hosts = [] + + db = opts[:db] || nil + timeout = opts[:timeout] || nil + + raise Error, "No hosts given" unless opts[:hosts] + + opts[:hosts].each do |h| + host, port = h.split(':') + hosts << Redis.new(:host => host, :port => port, :db => db, :timeout => timeout, :db => db) end - @ring = HashRing.new srvs + + @ring = HashRing.new hosts end def node_for_key(key) @@ -24,7 +31,7 @@ class DistRedis end def method_missing(sym, *args, &blk) - if redis = node_for_key(args.first) + if redis = node_for_key(args.first.to_s) redis.send sym, *args, &blk else super @@ -94,11 +101,11 @@ r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localho r.push_tail 'listor', 'foo4' r.push_tail 'listor', 'foo5' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') puts "key distribution:"