Class: Dalli::Client
- Inherits:
-
Object
- Object
- Dalli::Client
- Defined in:
- lib/dalli/client.rb,
lib/dalli/cas/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.
-
#alive! ⇒ Object
Make sure memcache servers are alive, or raise an Dalli::RingError.
-
#append(key, value) ⇒ Object
Append value to the value already stored on the server for ‘key’.
-
#cas(key, ttl = nil, options = nil) ⇒ 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
-
#delete_cas(key, cas = 0) ⇒ Object
Delete a key/value pair, verifying existing CAS.
- #fetch(key, ttl = nil, options = nil) ⇒ Object
- #flush(delay = 0) ⇒ Object (also: #flush_all)
-
#get(key, options = nil) ⇒ Object
Get the value associated with the key.
-
#get_cas(key) ⇒ Object
Get the value and CAS ID associated with the key.
-
#get_multi(*keys) ⇒ Object
Fetch multiple keys efficiently.
-
#get_multi_cas(*keys) ⇒ Object
Fetch multiple keys efficiently, including available metadata such as CAS.
-
#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.
-
#replace_cas(key, value, cas, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, verifying existing CAS, 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
-
#set_cas(key, value, cas, ttl = nil, options = nil) ⇒ Object
Set the key-value pair, verifying existing CAS.
-
#stats(type = nil) ⇒ Object
Collect the stats for each server.
-
#touch(key, ttl = nil) ⇒ Object
Touch updates expiration time for a given key.
-
#version ⇒ Object
Version of the memcache servers.
-
#with {|_self| ... } ⇒ Object
Stub method so a bare Dalli client can pretend to be a connection pool.
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', '/var/run/memcached/socket'],
: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. Dalli also supports the ability to connect to Memcached on localhost through a UNIX socket. To use this functionality, use a full pathname (beginning with a slash character ‘/’) in place of the “host:port” pair in the server configuration.
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 1024 bytes before sending them to memcached.
-
:serializer - defaults to Marshal
-
:compressor - defaults to zlib
31 32 33 34 35 |
# File 'lib/dalli/client.rb', line 31 def initialize(servers=nil, ={}) @servers = normalize_servers(servers || ENV["MEMCACHE_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 truthy if the operation succeeded.
113 114 115 116 |
# File 'lib/dalli/client.rb', line 113 def add(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:add, key, value, ttl, ) end |
#alive! ⇒ Object
Make sure memcache servers are alive, or raise an Dalli::RingError
221 222 223 |
# File 'lib/dalli/client.rb', line 221 def alive! ring.server_for_key("") 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.
133 134 135 |
# File 'lib/dalli/client.rb', line 133 def append(key, value) perform(:append, key, value.to_s) end |
#cas(key, ttl = nil, options = nil) ⇒ 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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/dalli/client.rb', line 95 def cas(key, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i (value, cas) = perform(:cas, key) value = (!value || value == 'Not found') ? nil : value if value newvalue = yield(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.
238 239 240 241 242 243 |
# File 'lib/dalli/client.rb', line 238 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.
182 183 184 185 186 |
# File 'lib/dalli/client.rb', line 182 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
126 127 128 |
# File 'lib/dalli/client.rb', line 126 def delete(key) perform(:delete, key, 0) end |
#delete_cas(key, cas = 0) ⇒ Object
Delete a key/value pair, verifying existing CAS. Returns true if succeeded, and falsy otherwise.
53 54 55 |
# File 'lib/dalli/cas/client.rb', line 53 def delete_cas(key, cas=0) perform(:delete, key, cas) end |
#fetch(key, ttl = nil, options = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/dalli/client.rb', line 74 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
144 145 146 147 |
# File 'lib/dalli/client.rb', line 144 def flush(delay=0) time = -delay ring.servers.map { |s| s.request(:flush, time += delay) } end |
#get(key, options = nil) ⇒ Object
Get the value associated with the key.
55 56 57 |
# File 'lib/dalli/client.rb', line 55 def get(key, =nil) perform(:get, key) end |
#get_cas(key) ⇒ Object
Get the value and CAS ID associated with the key. If a block is provided, value and CAS will be passed to the block.
8 9 10 11 12 13 14 15 16 |
# File 'lib/dalli/cas/client.rb', line 8 def get_cas(key) (value, cas) = perform(:cas, key) value = (!value || value == 'Not found') ? nil : value if block_given? yield value, cas else [value, cas] end end |
#get_multi(*keys) ⇒ Object
Fetch multiple keys efficiently. If a block is given, yields key/value pairs one at a time. Otherwise returns a hash of { ‘key’ => ‘value’, ‘key2’ => ‘value1’ }
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dalli/client.rb', line 63 def get_multi(*keys) return {} if keys.flatten.compact.empty? if block_given? get_multi_yielder(keys) {|k, data| yield k, data.first} else Hash.new.tap do |hash| get_multi_yielder(keys) {|k, data| hash[k] = data.first} end end end |
#get_multi_cas(*keys) ⇒ Object
Fetch multiple keys efficiently, including available metadata such as CAS. If a block is given, yields key/data pairs one a time. Data is an array:
- value, cas_id
-
If no block is given, returns a hash of
{ 'key' => [value, cas_id] }
24 25 26 27 28 29 30 31 32 |
# File 'lib/dalli/cas/client.rb', line 24 def get_multi_cas(*keys) if block_given? get_multi_yielder(keys) {|*args| yield(*args)} else Hash.new.tap do |hash| get_multi_yielder(keys) {|k, data| hash[k] = data} end 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.
162 163 164 165 166 |
# File 'lib/dalli/client.rb', line 162 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.
46 47 48 49 50 51 |
# File 'lib/dalli/client.rb', line 46 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.
140 141 142 |
# File 'lib/dalli/client.rb', line 140 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 truthy if the operation succeeded.
121 122 123 124 |
# File 'lib/dalli/client.rb', line 121 def replace(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:replace, key, value, ttl, 0, ) end |
#replace_cas(key, value, cas, ttl = nil, options = nil) ⇒ Object
Conditionally add a key/value pair, verifying existing CAS, only if the key already exists on the server. Returns the new CAS value if the operation succeeded, or falsy otherwise.
46 47 48 49 |
# File 'lib/dalli/cas/client.rb', line 46 def replace_cas(key, value, cas, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:replace, key, value, ttl, cas, ) end |
#reset_stats ⇒ Object
Reset stats for each server.
213 214 215 216 217 |
# File 'lib/dalli/client.rb', line 213 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
105 106 107 108 |
# File 'lib/dalli/client.rb', line 105 def set(key, value, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:set, key, value, ttl, 0, ) end |
#set_cas(key, value, cas, ttl = nil, options = nil) ⇒ Object
Set the key-value pair, verifying existing CAS. Returns the resulting CAS value if succeeded, and falsy otherwise.
37 38 39 40 |
# File 'lib/dalli/cas/client.rb', line 37 def set_cas(key, value, cas, ttl=nil, =nil) ttl ||= @options[:expires_in].to_i perform(:set, key, value, ttl, cas, ) end |
#stats(type = nil) ⇒ Object
Collect the stats for each server. You can optionally pass a type including :items, :slabs or :settings to get specific stats Returns a hash like { ‘hostname:port’ => { ‘stat1’ => ‘value1’, … }, ‘hostname2:port’ => { … } }
202 203 204 205 206 207 208 209 |
# File 'lib/dalli/client.rb', line 202 def stats(type=nil) type = nil if ![nil, :items,:slabs,:settings].include? type values = {} ring.servers.each do |server| values["#{server.name}"] = server.alive? ? server.request(:stats,type.to_s) : nil end values end |
#touch(key, ttl = nil) ⇒ Object
Touch updates expiration time for a given key.
Returns true if key exists, otherwise nil.
192 193 194 195 196 |
# File 'lib/dalli/client.rb', line 192 def touch(key, ttl=nil) ttl ||= @options[:expires_in].to_i resp = perform(:touch, key, ttl) resp.nil? ? nil : true end |
#version ⇒ Object
Version of the memcache servers.
227 228 229 230 231 232 233 |
# File 'lib/dalli/client.rb', line 227 def version values = {} ring.servers.each do |server| values["#{server.name}"] = server.alive? ? server.request(:version) : nil end values end |
#with {|_self| ... } ⇒ Object
Stub method so a bare Dalli client can pretend to be a connection pool.
247 248 249 |
# File 'lib/dalli/client.rb', line 247 def with yield self end |