Class: Redi::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/redi/mock.rb,
lib/redi/pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, mock = false) ⇒ Pool

Returns a new instance of Pool.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redi/pool.rb', line 25

def initialize(config,mock=false)
  key_type = Struct.new(:id, :to_s)

  # build server pool
  @bucket2server = {}
  buckets = []
  @servers = config.map {|cfg|
    bucket_range = cfg.delete(:buckets)
    s, e = bucket_range.split('-').map {|n| n.to_i }
    if mock
      require 'redi/mock'
      conn = Mock.new
    else
      conn = Redis.new(cfg)
    end
    (s..e).each do|i|
      bucket_name = "n#{i}"
      buckets << key_type.new(i, bucket_name)
      @bucket2server[bucket_name] = conn
    end
    conn
  }

  # create the keyring to map redis keys to buckets
  @keyring  = Redis::HashRing.new(buckets)
end

Instance Attribute Details

#keyspaceObject (readonly)

Returns the value of attribute keyspace.



24
25
26
# File 'lib/redi/pool.rb', line 24

def keyspace
  @keyspace
end

#serversObject (readonly)

Returns the value of attribute servers.



24
25
26
# File 'lib/redi/pool.rb', line 24

def servers
  @servers
end

Instance Method Details

#flushallObject



62
63
64
# File 'lib/redi/pool.rb', line 62

def flushall
  @servers.map {|s| s.flushall }
end

#flushdbObject



66
67
68
# File 'lib/redi/pool.rb', line 66

def flushdb
  @servers.each {|s| s.flushdb }
end

#mock!Object



13
14
15
16
17
18
# File 'lib/redi/mock.rb', line 13

def mock!
  @servers.map! {|s| Mock.new }
  @bucket2server.keys.each_with_index do|k,i|
    @bucket2server[k] = @servers[i % @servers.size]
  end
end

#qualified_key_for(key) ⇒ Object



52
53
54
55
# File 'lib/redi/pool.rb', line 52

def qualified_key_for(key)
  bucket = @keyring.get_node(key)
  "#{bucket.to_s}:#{key}"
end

#redis_by_key(key) ⇒ Object



57
58
59
60
# File 'lib/redi/pool.rb', line 57

def redis_by_key(key)
  bucket = @keyring.get_node(key)
  Redis::Namespace.new(bucket.to_s, :redis => @bucket2server[bucket.to_s])
end