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 => false, :marshal => false)

</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.
:marshal - ensure that the value you store is exactly what is returned.  Otherwise you can see this:
     set('abc', 123)
     get('abc') ==> '123'  (Note you set an Integer but got back a String)
   Default: true.


22
23
24
25
26
# File 'lib/dalli/client.rb', line 22

def initialize(servers=nil, options={})
  @servers = servers
  @options = options
  self.extend(Dalli::Marshal) unless options[:marshal] == false
end

Instance Method Details

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



69
70
71
# File 'lib/dalli/client.rb', line 69

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

#append(key, value) ⇒ Object



81
82
83
# File 'lib/dalli/client.rb', line 81

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

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



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

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

#closeObject Also known as: reset



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

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)


124
125
126
127
# File 'lib/dalli/client.rb', line 124

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



77
78
79
# File 'lib/dalli/client.rb', line 77

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

#fetch(key, ttl = 0) ⇒ Object



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

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

#flush(delay = 0) ⇒ Object



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

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.



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

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

#get(key) ⇒ Object

The standard memcached instruction set



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

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

#get_multi(*keys) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/dalli/client.rb', line 37

def get_multi(*keys)
  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); 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)


109
110
111
112
# File 'lib/dalli/client.rb', line 109

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



85
86
87
# File 'lib/dalli/client.rb', line 85

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

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



73
74
75
# File 'lib/dalli/client.rb', line 73

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

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



65
66
67
# File 'lib/dalli/client.rb', line 65

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

#statsObject



129
130
131
# File 'lib/dalli/client.rb', line 129

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