Class: Dalli::Client
- Inherits:
-
Object
- Object
- Dalli::Client
- Defined in:
- lib/dalli/client.rb
Instance Method Summary collapse
- #add(key, value, ttl = 0, options = nil) ⇒ Object
- #append(key, value) ⇒ Object
- #cas(key, ttl = 0, options = nil, &block) ⇒ Object
- #close ⇒ Object (also: #reset)
-
#decr(key, amt = 1, ttl = 0, default = nil) ⇒ Object
Decr subtracts the given amount from the counter on the memcached server.
- #delete(key) ⇒ Object
- #fetch(key, ttl = 0, options = nil) ⇒ Object
- #flush(delay = 0) ⇒ Object
-
#flush_all(delay = 0) ⇒ Object
deprecated, please use #flush.
-
#get(key, options = nil) ⇒ Object
The standard memcached instruction set.
- #get_multi(*keys) ⇒ Object
-
#incr(key, amt = 1, ttl = 0, default = nil) ⇒ Object
Incr adds the given amount to the counter on the memcached server.
-
#initialize(servers = nil, options = {}) ⇒ Client
constructor
Dalli::Client is the main class which developers will use to interact with the memcached server.
- #prepend(key, value) ⇒ Object
- #replace(key, value, ttl = 0, options = nil) ⇒ Object
- #set(key, value, ttl = 0, options = nil) ⇒ Object
- #stats ⇒ Object
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, ={}) @servers = servers || 'localhost:11211' @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, =nil) perform(:add, key, serialize(value, ), 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, =nil, &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 |
#close ⇒ Object 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 = 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.
122 123 124 125 |
# File 'lib/dalli/client.rb', line 122 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
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, =nil) val = get(key, ) if val.nil? && block_given? val = yield add(key, val, ttl, ) 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, =nil) resp = perform(:get, key) (!resp || resp == 'Not found') ? nil : deserialize(resp, ) 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? = nil = 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, ); 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.
107 108 109 110 |
# File 'lib/dalli/client.rb', line 107 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 |
#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, =nil) perform(:replace, key, serialize(value, ), 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, =nil) perform(:set, key, serialize(value, ), ttl) end |
#stats ⇒ Object
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 |