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 || env_servers || 'localhost:11211'
  @options = options
end

Instance Method Details

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



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

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

#append(key, value) ⇒ Object



91
92
93
# File 'lib/dalli/client.rb', line 91

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

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



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

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



143
144
145
146
147
148
# File 'lib/dalli/client.rb', line 143

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

#decr(key, amt = 1, 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)


134
135
136
137
# File 'lib/dalli/client.rb', line 134

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

#delete(key) ⇒ Object



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

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

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



57
58
59
60
61
62
63
64
# File 'lib/dalli/client.rb', line 57

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



99
100
101
102
# File 'lib/dalli/client.rb', line 99

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.



105
106
107
# File 'lib/dalli/client.rb', line 105

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

#get(key, options = nil) ⇒ Object



39
40
41
42
# File 'lib/dalli/client.rb', line 39

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

#get_multi(*keys) ⇒ Object



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

def get_multi(*keys)
  return {} if keys.empty?
  options = nil
  options = keys.pop if keys.last.is_a?(Hash) || keys.last.nil?
  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 = 1, 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)


119
120
121
122
# File 'lib/dalli/client.rb', line 119

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

#multiObject

Turn on quiet aka noreply support. All relevant operations within this block with be effectively pipelined as Dalli will use ‘quiet’ operations where possible. Currently supports the set, add, replace and delete operations.



32
33
34
35
36
37
# File 'lib/dalli/client.rb', line 32

def multi
  Thread.current[:multi] = true
  yield
ensure
  Thread.current[:multi] = nil
end

#prepend(key, value) ⇒ Object



95
96
97
# File 'lib/dalli/client.rb', line 95

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

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



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

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

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



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

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

#statsObject



139
140
141
# File 'lib/dalli/client.rb', line 139

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