Class: Dalli::Client
- Inherits:
-
Object
- Object
- Dalli::Client
- Defined in:
- lib/dalli/client.rb
Instance Method Summary collapse
-
#add(key, value, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, if the key does not already exist on the server.
-
#append(key, value) ⇒ Object
Append value to the value already stored on the server for ‘key’.
-
#cas(key, ttl = nil, options = nil, &block) ⇒ Object
compare and swap values using optimistic locking.
-
#close ⇒ Object
(also: #reset)
Close our connection to each server.
-
#decr(key, amt = 1, ttl = nil, default = nil) ⇒ Object
Decr subtracts the given amount from the counter on the memcached server.
- #delete(key) ⇒ Object
- #fetch(key, ttl = nil, options = nil) ⇒ Object
- #flush(delay = 0) ⇒ Object (also: #flush_all)
- #get(key, options = nil) ⇒ Object
-
#get_multi(*keys) ⇒ Object
Fetch multiple keys efficiently.
-
#incr(key, amt = 1, ttl = nil, 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.
-
#multi ⇒ Object
Turn on quiet aka noreply support.
-
#prepend(key, value) ⇒ Object
Prepend value to the value already stored on the server for ‘key’.
-
#replace(key, value, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, only if the key already exists on the server.
-
#reset_stats ⇒ Object
Reset stats for each server.
- #set(key, value, ttl = nil, options = nil) ⇒ Object
-
#stats ⇒ Object
Collect the stats for each server.
-
#touch(key, ttl = nil) ⇒ Object
Touch updates expiration time for a given key.
Constructor Details
#initialize(servers = nil, options = {}) ⇒ Client
Dalli::Client is the main class which developers will use to interact with the memcached server. Usage:
Dalli::Client.new(['localhost:11211:10', 'cache-2.example.com:11211:5', '192.168.0.1:22122:5'],
:threadsafe => true, :failover => true, :expires_in => 300)
servers is an Array of “host:port:weight” where weight allows you to distribute cache unevenly. Both weight and port are optional. If you pass in nil, Dalli will use the MEMCACHE_SERVERS
environment variable or default to ‘localhost:11211’ if it is not present.
Options:
-
:namespace - prepend each key with this value to provide simple namespacing.
-
:failover - if a server is down, look for and store values on another server in the ring. Default: true.
-
:threadsafe - ensure that only one thread is actively using a socket at a time. Default: true.
-
:expires_in - default TTL in seconds if you do not pass TTL as a parameter to an individual operation, defaults to 0 or forever
-
:compress - defaults to false, if true Dalli will compress values larger than 100 bytes before sending them to memcached.
26 27 28 29 30 |
# File 'lib/dalli/client.rb', line 26 def initialize(servers=nil, ={}) @servers = servers || env_servers || '127.0.0.1:11211' @options = () @ring = nil end |
Instance Method Details
#add(key, value, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, if the key does not already exist on the server. Returns true if the operation succeeded.
125 126 127 128 |
# File 'lib/dalli/client.rb', line 125 def add(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:add, key, value, ttl, ) end |
#append(key, value) ⇒ Object
Append value to the value already stored on the server for ‘key’. Appending only works for values stored with :raw => true.
145 146 147 |
# File 'lib/dalli/client.rb', line 145 def append(key, value) perform(:append, key, value.to_s) end |
#cas(key, ttl = nil, options = nil, &block) ⇒ Object
compare and swap values using optimistic locking. Fetch the existing value for key. If it exists, yield the value to the block. Add the block’s return value as the new value for the key. Add will fail if someone else changed the value.
Returns:
-
nil if the key did not exist.
-
false if the value was changed by someone else.
-
true if the value was successfully updated.
107 108 109 110 111 112 113 114 115 |
# File 'lib/dalli/client.rb', line 107 def cas(key, ttl=nil, =nil, &block) ttl ||= @options[:expires_in].to_i (value, cas) = perform(:cas, key) value = (!value || value == 'Not found') ? nil : value if value newvalue = block.call(value) perform(:set, key, newvalue, ttl, cas, ) end end |
#close ⇒ Object Also known as: reset
Close our connection to each server. If you perform another operation after this, the connections will be re-established.
232 233 234 235 236 237 |
# File 'lib/dalli/client.rb', line 232 def close if @ring @ring.servers.each { |s| s.close } @ring = nil end end |
#decr(key, amt = 1, ttl = nil, default = nil) ⇒ Object
Decr subtracts the given amount from the counter on the memcached server. Amt must be a positive integer 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.
Note that the ttl will only apply if the counter does not already exist. To decrease an existing counter and update its TTL, use #cas.
194 195 196 197 198 |
# File 'lib/dalli/client.rb', line 194 def decr(key, amt=1, ttl=nil, default=nil) raise ArgumentError, "Positive values only: #{amt}" if amt < 0 ttl ||= @options[:expires_in].to_i perform(:decr, key, amt.to_i, ttl, default) end |
#delete(key) ⇒ Object
138 139 140 |
# File 'lib/dalli/client.rb', line 138 def delete(key) perform(:delete, key) end |
#fetch(key, ttl = nil, options = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/dalli/client.rb', line 86 def fetch(key, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i val = get(key, ) if val.nil? && block_given? val = yield add(key, val, ttl, ) end val end |
#flush(delay = 0) ⇒ Object Also known as: flush_all
156 157 158 159 |
# File 'lib/dalli/client.rb', line 156 def flush(delay=0) time = -delay ring.servers.map { |s| s.request(:flush, time += delay) } end |
#get(key, options = nil) ⇒ Object
48 49 50 51 |
# File 'lib/dalli/client.rb', line 48 def get(key, =nil) resp = perform(:get, key) resp.nil? || resp == 'Not found' ? nil : resp end |
#get_multi(*keys) ⇒ Object
Fetch multiple keys efficiently. Returns a hash of { ‘key’ => ‘value’, ‘key2’ => ‘value1’ }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dalli/client.rb', line 56 def get_multi(*keys) return {} if keys.empty? = nil = keys.pop if keys.last.is_a?(Hash) || keys.last.nil? ring.lock do keys.flatten.each do |key| begin perform(:getkq, key) rescue DalliError, NetworkError => e Dalli.logger.debug { e. } Dalli.logger.debug { "unable to get key #{key}" } end end values = {} ring.servers.each do |server| next unless server.alive? begin server.request(:noop).each_pair do |key, value| values[key_without_namespace(key)] = value end rescue DalliError, NetworkError => e Dalli.logger.debug { e. } Dalli.logger.debug { "results from this server will be missing" } end end values end end |
#incr(key, amt = 1, ttl = nil, default = nil) ⇒ Object
Incr adds the given amount to the counter on the memcached server. Amt must be a positive integer value.
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.
Note that the ttl will only apply if the counter does not already exist. To increase an existing counter and update its TTL, use #cas.
174 175 176 177 178 |
# File 'lib/dalli/client.rb', line 174 def incr(key, amt=1, ttl=nil, default=nil) raise ArgumentError, "Positive values only: #{amt}" if amt < 0 ttl ||= @options[:expires_in].to_i perform(:incr, key, amt.to_i, ttl, default) end |
#multi ⇒ Object
Turn on quiet aka noreply support. All relevant operations within this block will be effectively pipelined as Dalli will use ‘quiet’ operations where possible. Currently supports the set, add, replace and delete operations.
41 42 43 44 45 46 |
# File 'lib/dalli/client.rb', line 41 def multi old, Thread.current[:dalli_multi] = Thread.current[:dalli_multi], true yield ensure Thread.current[:dalli_multi] = old end |
#prepend(key, value) ⇒ Object
Prepend value to the value already stored on the server for ‘key’. Prepending only works for values stored with :raw => true.
152 153 154 |
# File 'lib/dalli/client.rb', line 152 def prepend(key, value) perform(:prepend, key, value.to_s) end |
#replace(key, value, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, only if the key already exists on the server. Returns true if the operation succeeded.
133 134 135 136 |
# File 'lib/dalli/client.rb', line 133 def replace(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:replace, key, value, ttl, ) end |
#reset_stats ⇒ Object
Reset stats for each server.
223 224 225 226 227 |
# File 'lib/dalli/client.rb', line 223 def reset_stats ring.servers.map do |server| server.alive? ? server.request(:reset_stats) : nil end end |
#set(key, value, ttl = nil, options = nil) ⇒ Object
117 118 119 120 |
# File 'lib/dalli/client.rb', line 117 def set(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:set, key, value, ttl, 0, ) end |
#stats ⇒ Object
Collect the stats for each server. Returns a hash like { ‘hostname:port’ => { ‘stat1’ => ‘value1’, … }, ‘hostname2:port’ => { … } }
213 214 215 216 217 218 219 |
# File 'lib/dalli/client.rb', line 213 def stats values = {} ring.servers.each do |server| values["#{server.hostname}:#{server.port}"] = server.alive? ? server.request(:stats) : nil end values end |
#touch(key, ttl = nil) ⇒ Object
Touch updates expiration time for a given key.
Returns true if key exists, otherwise nil.
204 205 206 207 208 |
# File 'lib/dalli/client.rb', line 204 def touch(key, ttl=nil) ttl ||= @options[:expires_in].to_i resp = perform(:touch, key, ttl) resp.nil? ? nil : true end |