Class: Dalli::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs) ⇒ Server

Returns a new instance of Server.

Raises:

  • (NotImplementedError)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dalli/server.rb', line 10

def initialize(attribs)
  (@hostname, @port, @weight) = attribs.split(':')
  @port ||= 11211
  @port = Integer(@port)
  @weight ||= 1
  @weight = Integer(@weight)
  @down_at = nil
  connection
  @version = detect_memcached_version
  raise NotImplementedError, "Dalli does not support memcached versions < 1.4.0, found #{@version} at #{@hostname}:#{@port}" if @version < '1.4.0'
  Dalli.logger.debug { "#{@hostname}:#{@port} running memcached v#{@version}" }
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



6
7
8
# File 'lib/dalli/server.rb', line 6

def hostname
  @hostname
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#weightObject

Returns the value of attribute weight.



8
9
10
# File 'lib/dalli/server.rb', line 8

def weight
  @weight
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dalli/server.rb', line 38

def alive?
  return true if @sock && !@sock.closed?
  return false if @down_at && @down_at == Time.now.to_i

  begin
    # try to reconnect at most once per second
    connection
    true
  rescue Dalli::NetworkError => dne
    Dalli.logger.info("Unable to connect to #{hostname}:#{port}: #{dne.message}")
    down!
  end
end

#closeObject



52
53
54
# File 'lib/dalli/server.rb', line 52

def close
  (@sock.close rescue nil; @sock = nil) if @sock
end

#lock!Object



56
57
# File 'lib/dalli/server.rb', line 56

def lock!
end

#request(op, *args) ⇒ Object

Chokepoint method for instrumentation



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dalli/server.rb', line 24

def request(op, *args)
  begin
    send(op, *args)
  rescue Dalli::NetworkError
    raise
  rescue Dalli::DalliError
    raise
  rescue Exception => ex
    Dalli.logger.error "Unexpected exception in Dalli: #{ex.class.name}: #{ex.message}"
    Dalli.logger.error ex.backtrace.join("\n\t")
    down!
  end
end

#unlock!Object



59
60
# File 'lib/dalli/server.rb', line 59

def unlock!
end