Class: DistRedis

Inherits:
Object
  • Object
show all
Defined in:
lib/dist_redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DistRedis

Returns a new instance of DistRedis.

Raises:

  • (Error)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dist_redis.rb', line 5

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 hosts 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/dist_redis.rb', line 33

def method_missing(sym, *args, &blk)
  if redis = node_for_key(args.first.to_s)
    redis.send sym, *args, &blk
  else
    super
  end
end

Instance Attribute Details

#ringObject (readonly)

Returns the value of attribute ring.



4
5
6
# File 'lib/dist_redis.rb', line 4

def ring
  @ring
end

Instance Method Details

#add_server(server) ⇒ Object



28
29
30
31
# File 'lib/dist_redis.rb', line 28

def add_server(server)
  server, port = server.split(':')
  @ring.add_node Redis.new(:host => server, :port => port)
end

#bgsaveObject



55
56
57
58
59
# File 'lib/dist_redis.rb', line 55

def bgsave
  @ring.nodes.each do |red|
    red.bgsave
  end
end

#delete_cloud!Object



67
68
69
70
71
72
73
# File 'lib/dist_redis.rb', line 67

def delete_cloud!
  @ring.nodes.each do |red|
    red.keys("*").each do |key|
      red.delete key
    end  
  end
end

#keys(glob) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/dist_redis.rb', line 41

def keys(glob)
  keyz = []
  @ring.nodes.each do |red|
    keyz.concat red.keys(glob)
  end
  keyz
end

#node_for_key(key) ⇒ Object



21
22
23
24
25
26
# File 'lib/dist_redis.rb', line 21

def node_for_key(key)
  if key =~ /\{(.*)?\}/
    key = $1
  end
  @ring.get_node(key)
end

#quitObject



61
62
63
64
65
# File 'lib/dist_redis.rb', line 61

def quit
  @ring.nodes.each do |red|
    red.quit
  end
end

#saveObject



49
50
51
52
53
# File 'lib/dist_redis.rb', line 49

def save
  @ring.nodes.each do |red|
    red.save
  end
end