Class: Dalli::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(servers = nil, options = {}) ⇒ Client

Dalli::Client is the main class which developers will use to interact with the memcached server. Usage: <pre> Dalli::Client.new([‘localhost:11211:10’, ‘cache-2.example.com:11211:5’, ‘192.168.0.1:22122:5’],

:threadsafe => true, :failover => true)

</pre> servers is an Array of “host:port:weight” where weight allows you to distribute cache unevenly. Both weight and port are optional.

Options:

:failover - if a server is down, store the value on another server.  Default: true.
:threadsafe - ensure that only one thread is actively using a socket at a time. Default: true.


18
19
20
21
# File 'lib/dalli/client.rb', line 18

def initialize(servers=nil, options={})
  @servers = servers
  @options = options
end

Instance Method Details

#add(key, value, ttl = 0, options = nil) ⇒ Object



67
68
69
# File 'lib/dalli/client.rb', line 67

def add(key, value, ttl=0, options=nil)
  perform(:add, key, serialize(value, options), ttl, 0)
end

#append(key, value) ⇒ Object



79
80
81
# File 'lib/dalli/client.rb', line 79

def append(key, value)
  perform(:append, key, value.to_s)
end

#cas(key, ttl = 0, options = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dalli/client.rb', line 54

def cas(key, ttl=0, options=nil, &block)
  (value, cas) = perform(:cas, key)
  value = (!value || value == 'Not found') ? nil : deserialize(value, options)
  if value
    newvalue = block.call(value)
    perform(:add, key, serialize(newvalue, options), ttl, cas)
  end
end

#closeObject Also known as: reset



131
132
133
134
135
136
# File 'lib/dalli/client.rb', line 131

def close
  if @ring
    @ring.servers.map { |s| s.close }
    @ring = nil
  end
end

#decr(key, amt, ttl = 0, default = nil) ⇒ Object

Decr subtracts the given amount from the counter on the memcached server. Amt must be a positive value.

memcached counters are unsigned and cannot hold negative values. Calling decr on a counter which is 0 will just return 0.

If default is nil, the counter must already exist or the operation will fail and will return nil. Otherwise this method will return the new value for the counter.

Raises:

  • (ArgumentError)


122
123
124
125
# File 'lib/dalli/client.rb', line 122

def decr(key, amt, ttl=0, default=nil)
  raise ArgumentError, "Positive values only: #{amt}" if amt < 0
  perform(:decr, key, amt, ttl, default)
end

#delete(key) ⇒ Object



75
76
77
# File 'lib/dalli/client.rb', line 75

def delete(key)
  perform(:delete, key)
end

#fetch(key, ttl = 0, options = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/dalli/client.rb', line 45

def fetch(key, ttl=0, options=nil)
  val = get(key, options)
  if val.nil? && block_given?
    val = yield
    add(key, val, ttl, options)
  end
  val
end

#flush(delay = 0) ⇒ Object



87
88
89
90
# File 'lib/dalli/client.rb', line 87

def flush(delay=0)
  time = -delay
  ring.servers.map { |s| s.request(:flush, time += delay) }
end

#flush_all(delay = 0) ⇒ Object

deprecated, please use #flush.



93
94
95
# File 'lib/dalli/client.rb', line 93

def flush_all(delay=0)
  flush(delay)
end

#get(key, options = nil) ⇒ Object

The standard memcached instruction set



27
28
29
30
# File 'lib/dalli/client.rb', line 27

def get(key, options=nil)
  resp = perform(:get, key)
  (!resp || resp == 'Not found') ? nil : deserialize(resp, options)
end

#get_multi(*keys) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dalli/client.rb', line 32

def get_multi(*keys)
  return {} if keys.empty?
  options = nil
  options = keys.pop if keys.last.is_a?(Hash)
  ring.lock do
    keys.flatten.each do |key|
      perform(:getkq, key)
    end
    values = ring.servers.inject({}) { |hash, s| hash.merge!(s.request(:noop)); hash }
    values.inject(values) { |memo, (k,v)| memo[k] = deserialize(v, options); memo }
  end
end

#incr(key, amt, ttl = 0, default = nil) ⇒ Object

Incr adds the given amount to the counter on the memcached server. Amt must be a positive value.

memcached counters are unsigned and cannot hold negative values. Calling decr on a counter which is 0 will just return 0.

If default is nil, the counter must already exist or the operation will fail and will return nil. Otherwise this method will return the new value for the counter.

Raises:

  • (ArgumentError)


107
108
109
110
# File 'lib/dalli/client.rb', line 107

def incr(key, amt, ttl=0, default=nil)
  raise ArgumentError, "Positive values only: #{amt}" if amt < 0
  perform(:incr, key, amt, ttl, default)
end

#prepend(key, value) ⇒ Object



83
84
85
# File 'lib/dalli/client.rb', line 83

def prepend(key, value)
  perform(:prepend, key, value.to_s)
end

#replace(key, value, ttl = 0, options = nil) ⇒ Object



71
72
73
# File 'lib/dalli/client.rb', line 71

def replace(key, value, ttl=0, options=nil)
  perform(:replace, key, serialize(value, options), ttl)
end

#set(key, value, ttl = 0, options = nil) ⇒ Object



63
64
65
# File 'lib/dalli/client.rb', line 63

def set(key, value, ttl=0, options=nil)
  perform(:set, key, serialize(value, options), ttl)
end

#statsObject



127
128
129
# File 'lib/dalli/client.rb', line 127

def stats
  ring.servers.inject({}) { |memo, s| memo["#{s.hostname}:#{s.port}"] = s.request(:stats); memo }
end