Class: Dalli::ElastiCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/elasticache.rb,
lib/dalli/elasticache/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_endpoint, options = {}) ⇒ ElastiCache

Returns a new instance of ElastiCache.



9
10
11
12
13
14
# File 'lib/dalli/elasticache.rb', line 9

def initialize(config_endpoint, options={})
  @config_host, @config_port = config_endpoint.split(':')
  @config_port ||= 11211
  @options = options

end

Instance Attribute Details

#config_hostObject

Returns the value of attribute config_host.



7
8
9
# File 'lib/dalli/elasticache.rb', line 7

def config_host
  @config_host
end

#config_portObject

Returns the value of attribute config_port.



7
8
9
# File 'lib/dalli/elasticache.rb', line 7

def config_port
  @config_port
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/dalli/elasticache.rb', line 7

def options
  @options
end

Instance Method Details

#clientObject



16
17
18
# File 'lib/dalli/elasticache.rb', line 16

def client
  Dalli::Client.new(servers, options)
end

#config_get_clusterObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dalli/elasticache.rb', line 28

def config_get_cluster
  # TODO: handle timeouts
  s = TCPSocket.new(config_host, config_port)
  s.puts "config get cluster\r\n"
  data = []
  while (line = s.gets) != "END\r\n"
    data << line
  end

  s.close
  data
end

#dataObject



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

def data
  return @data if @data
  raw_data = config_get_cluster
  version = raw_data[1].to_i
  instance_data = raw_data[2].split(/\s+/)
  instances = instance_data.map{ |raw| host, ip, port = raw.split('|'); {:host => host, :ip => ip, :port => port} }
  @data = { :version => version, :instances => instances }
end

#refreshObject



20
21
22
23
24
25
26
# File 'lib/dalli/elasticache.rb', line 20

def refresh
  # Reset data
  @data = nil
  data

  self
end

#serversObject



54
55
56
# File 'lib/dalli/elasticache.rb', line 54

def servers
  data[:instances].map{ |i| "#{i[:ip]}:#{i[:port]}" }
end

#versionObject



50
51
52
# File 'lib/dalli/elasticache.rb', line 50

def version
  data[:version]
end