Class: Dalli::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/client.rb

Overview

Dalli::Client is the main class which developers will use to interact with Memcached.

Constant Summary collapse

ALLOWED_STAT_KEYS =
i[items slabs settings].freeze
CACHE_NILS =
{ cache_nils: true }.freeze

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:

Dalli::Client.new(['localhost:11211:10',
                   'cache-2.example.com:11211:5',
                   '192.168.0.1:22122:5',
                   '/var/run/memcached/socket'],
                  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 - if true Dalli will compress values larger than compression_min_size bytes before sending them

    to memcached.  Default: true.
    
  • :compression_min_size - the minimum size (in bytes) for which Dalli will compress values sent to Memcached.

    Defaults to 4K.
    
  • :serializer - defaults to Marshal

  • :compressor - defaults to Dalli::Compressor, a Zlib-based implementation

  • :cache_nils - defaults to false, if true Dalli will not treat cached nil values as ‘not found’ for

    #fetch operations.
    
  • :raw - If set, disables serialization and compression entirely at the client level.

    Only String values are supported. This is useful when the caller handles its own
    serialization (e.g., Rails' ActiveSupport::Cache). Note: this is different from
    the per-request :raw option which converts values to strings but still uses the
    serialization pipeline.
    
  • :digest_class - defaults to Digest::MD5, allows you to pass in an object that responds to the hexdigest method,

    useful for injecting a FIPS compliant hash object.
    
  • :otel_db_statement - controls the db.query.text span attribute when OpenTelemetry is loaded.

    +:include+ logs the full operation and key(s), +:obfuscate+ replaces keys with "?",
    +nil+ (default) omits the attribute entirely.
    
  • :otel_peer_service - when set, adds a peer.service span attribute with this value for logical service naming.



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

def initialize(servers = nil, options = {})
  @normalized_servers = ::Dalli::ServersArgNormalizer.normalize_servers(servers)
  @options = normalize_options(options)
  warn_removed_options(@options)
  @key_manager = ::Dalli::KeyManager.new(@options)
  @ring = nil
end

Instance Method Details

#add(key, value, ttl = nil, req_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.



328
329
330
# File 'lib/dalli/client.rb', line 328

def add(key, value, ttl = nil, req_options = nil)
  perform(:add, key, value, ttl_or_default(ttl), req_options)
end

#alive!Object

Make sure memcache servers are alive, or raise an Dalli::RingError



474
475
476
# File 'lib/dalli/client.rb', line 474

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.



378
379
380
# File 'lib/dalli/client.rb', line 378

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

#cache_nilsObject



493
494
495
# File 'lib/dalli/client.rb', line 493

def cache_nils
  @options[:cache_nils]
end

#cas(key, ttl = nil, req_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.



252
253
254
# File 'lib/dalli/client.rb', line 252

def cas(key, ttl = nil, req_options = nil, &)
  cas_core(key, false, ttl, req_options, &)
end

#cas!(key, ttl = nil, req_options = nil) ⇒ Object

like #cas, but will yield to the block whether or not the value already exists.

Returns:

  • false if the value was changed by someone else.

  • true if the value was successfully updated.



263
264
265
# File 'lib/dalli/client.rb', line 263

def cas!(key, ttl = nil, req_options = nil, &)
  cas_core(key, true, ttl, req_options, &)
end

#closeObject Also known as: reset

Close our connection to each server. If you perform another operation after this, the connections will be re-established.



481
482
483
484
# File 'lib/dalli/client.rb', line 481

def close
  @ring&.close
  @ring = nil
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.

If the value already exists, it must have been set with raw: true



424
425
426
427
428
# File 'lib/dalli/client.rb', line 424

def decr(key, amt = 1, ttl = nil, default = nil)
  check_positive!(amt)

  perform(:decr, key, amt.to_i, ttl_or_default(ttl), default)
end

#delete(key) ⇒ Object



353
354
355
# File 'lib/dalli/client.rb', line 353

def delete(key)
  delete_cas(key, 0)
end

#delete_cas(key, cas = 0) ⇒ Object

Delete a key/value pair, verifying existing CAS. Returns true if succeeded, and falsy otherwise.



349
350
351
# File 'lib/dalli/client.rb', line 349

def delete_cas(key, cas = 0)
  perform(:delete, key, cas)
end

#delete_multi(keys) ⇒ void

This method returns an undefined value.

Delete multiple keys efficiently using pipelining. This method is more efficient than calling delete() in a loop because it batches requests by server and uses quiet mode.

Example:

client.delete_multi(['key1', 'key2', 'key3'])

Parameters:

  • keys to delete



367
368
369
370
371
372
373
# File 'lib/dalli/client.rb', line 367

def delete_multi(keys)
  return if keys.empty?

  Instrumentation.trace('delete_multi', multi_trace_attrs('delete_multi', keys.size, keys)) do
    pipelined_deleter.process(keys)
  end
end

#fetch(key, ttl = nil, req_options = nil) ⇒ Object

Fetch the value associated with the key. If a value is found, then it is returned.

If a value is not found and no block is given, then nil is returned.

If a value is not found (or if the found value is nil and :cache_nils is false) and a block is given, the block will be invoked and its return value written to the cache and returned.



185
186
187
188
189
190
191
192
193
# File 'lib/dalli/client.rb', line 185

def fetch(key, ttl = nil, req_options = nil)
  req_options = req_options.nil? ? CACHE_NILS : req_options.merge(CACHE_NILS) if cache_nils
  val = get(key, req_options)
  return val unless block_given? && not_found?(val)

  new_val = yield
  add(key, new_val, ttl_or_default(ttl), req_options)
  new_val
end

#fetch_with_lock(key, ttl: nil, lock_ttl: 30, recache_threshold: nil, req_options: nil) { ... } ⇒ Object

Fetch the value with thundering herd protection using the meta protocol’s N (vivify) and R (recache) flags.

This method prevents multiple clients from simultaneously regenerating the same cache entry (the “thundering herd” problem). Only one client wins the right to regenerate; other clients receive the stale value (if available) or wait.

Examples:

Basic usage

client.fetch_with_lock('expensive_key', ttl: 300, lock_ttl: 30) do
  expensive_database_query
end

With proactive recaching (recache before expiry)

client.fetch_with_lock('key', ttl: 300, lock_ttl: 30, recache_threshold: 60) do
  expensive_operation
end

Parameters:

  • the cache key

  • (defaults to: nil)

    time-to-live for the cached value in seconds

  • (defaults to: 30)

    how long the lock/stub lives (default: 30 seconds) This is the maximum time other clients will return stale data while waiting for regeneration. Should be longer than your expected regeneration time.

  • (defaults to: nil)

    if set, win the recache race when the item’s remaining TTL is below this threshold. Useful for proactive recaching.

  • (defaults to: nil)

    options passed to set operations (e.g., raw: true)

Yields:

  • Block to regenerate the value (only called if this client won the race)

Returns:

  • the cached value (may be stale if another client is regenerating)



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dalli/client.rb', line 225

def fetch_with_lock(key, ttl: nil, lock_ttl: 30, recache_threshold: nil, req_options: nil, &block)
  raise ArgumentError, 'Block is required for fetch_with_lock' unless block_given?

  key = key.to_s
  key = @key_manager.validate_key(key)

  server = ring.server_for_key(key)
  Instrumentation.trace('fetch_with_lock', trace_attrs('fetch_with_lock', key, server)) do
    fetch_with_lock_request(key, ttl, lock_ttl, recache_threshold, req_options, &block)
  end
rescue NetworkError => e
  Dalli.logger.debug { e.inspect }
  Dalli.logger.debug { 'retrying fetch_with_lock with new server' }
  retry
end

#flush(delay = 0) ⇒ Object Also known as: flush_all

Flush the memcached server, at ‘delay’ seconds in the future. Delay defaults to zero seconds, which means an immediate flush.



434
435
436
# File 'lib/dalli/client.rb', line 434

def flush(delay = 0)
  ring.servers.map { |s| s.request(:flush, delay) }
end

#gat(key, ttl = nil) ⇒ Object

Gat (get and touch) fetch an item and simultaneously update its expiration time.

If a value is not found, then nil is returned.



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

def gat(key, ttl = nil)
  perform(:gat, key, ttl_or_default(ttl))
end

#get(key, req_options = nil) ⇒ Object

Get the value associated with the key. If a value is not found, then nil is returned.



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

def get(key, req_options = nil)
  perform(:get, key, req_options)
end

#get_cas(key) {|value, cas| ... } ⇒ 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.

Yields:



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

def get_cas(key)
  (value, cas) = perform(:cas, key)
  return [value, cas] unless block_given?

  yield value, cas
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’ } rubocop:disable Style/ExplicitBlockArgument



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dalli/client.rb', line 148

def get_multi(*keys)
  keys.flatten!
  keys.compact!
  return {} if keys.empty?

  if block_given?
    get_multi_yielding(keys) { |k, v| yield k, v }
  else
    get_multi_hash(keys)
  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] }


167
168
169
170
171
172
173
174
175
# File 'lib/dalli/client.rb', line 167

def get_multi_cas(*keys)
  if block_given?
    pipelined_getter.process(keys) { |*args| yield(*args) }
  else
    {}.tap do |hash|
      pipelined_getter.process(keys) { |k, data| hash[k] = data }
    end
  end
end

#get_with_metadata(key, options = {}) ⇒ Hash

Get value with extended metadata.

Examples:

Get with hit status

result = client.('key', return_hit_status: true)
# => { value: "data", cas: 123, hit_before: true }

Get with all metadata without affecting LRU

result = client.('key',
  return_hit_status: true,
  return_last_access: true,
  skip_lru_bump: true
)
# => { value: "data", cas: 123, hit_before: true, last_access: 42 }

Parameters:

  • the cache key

  • (defaults to: {})

    options controlling what metadata to return

    • :return_cas [Boolean] return the CAS value (default: true)

    • :return_hit_status [Boolean] return whether item was previously accessed

    • :return_last_access [Boolean] return seconds since last access

    • :skip_lru_bump [Boolean] don’t bump LRU or update access stats

Returns:

  • containing:

    • :value - the cached value (or nil on miss)

    • :cas - the CAS value

    • :hit_before - true/false if previously accessed (only if return_hit_status: true)

    • :last_access - seconds since last access (only if return_last_access: true)



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dalli/client.rb', line 129

def (key, options = {})
  key = key.to_s
  key = @key_manager.validate_key(key)

  server = ring.server_for_key(key)
  Instrumentation.trace('get_with_metadata', trace_attrs('get_with_metadata', key, server)) do
    server.request(:meta_get, key, options)
  end
rescue NetworkError => e
  Dalli.logger.debug { e.inspect }
  Dalli.logger.debug { 'retrying get_with_metadata with new server' }
  retry
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.

If the value already exists, it must have been set with raw: true



402
403
404
405
406
# File 'lib/dalli/client.rb', line 402

def incr(key, amt = 1, ttl = nil, default = nil)
  check_positive!(amt)

  perform(:incr, key, amt.to_i, ttl_or_default(ttl), default)
end

#not_found?(val) ⇒ Boolean

Returns:



489
490
491
# File 'lib/dalli/client.rb', line 489

def not_found?(val)
  cache_nils ? val == ::Dalli::NOT_FOUND : val.nil?
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.



385
386
387
# File 'lib/dalli/client.rb', line 385

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

#quietObject Also known as: multi

Turn on quiet aka noreply support for a number of memcached operations.

All relevant operations within this block will be effectively pipelined as Dalli will use ‘quiet’ versions. The invoked methods will all return nil, rather than their usual response. Method latency will be substantially lower, as the caller will not be blocking on responses.

Currently supports storage (set, add, replace, append, prepend), arithmetic (incr, decr), flush and delete operations. Use of unsupported operations inside a block will raise an error.

Any error replies will be discarded at the end of the block, and Dalli client methods invoked inside the block will not have return values



284
285
286
287
288
289
290
291
# File 'lib/dalli/client.rb', line 284

def quiet
  old = Thread.current[::Dalli::QUIET]
  Thread.current[::Dalli::QUIET] = true
  yield
ensure
  @ring&.pipeline_consume_and_ignore_responses
  Thread.current[::Dalli::QUIET] = old
end

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

Conditionally add a key/value pair, only if the key already exists on the server. Returns truthy if the operation succeeded.



335
336
337
# File 'lib/dalli/client.rb', line 335

def replace(key, value, ttl = nil, req_options = nil)
  replace_cas(key, value, 0, ttl, req_options)
end

#replace_cas(key, value, cas, ttl = nil, req_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.



343
344
345
# File 'lib/dalli/client.rb', line 343

def replace_cas(key, value, cas, ttl = nil, req_options = nil)
  perform(:replace, key, value, ttl_or_default(ttl), cas, req_options)
end

#reset_statsObject

Reset stats for each server.



456
457
458
459
460
# File 'lib/dalli/client.rb', line 456

def reset_stats
  ring.servers.map do |server|
    server.alive? ? server.request(:reset_stats) : nil
  end
end

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



294
295
296
# File 'lib/dalli/client.rb', line 294

def set(key, value, ttl = nil, req_options = nil)
  set_cas(key, value, 0, ttl, req_options)
end

#set_cas(key, value, cas, ttl = nil, req_options = nil) ⇒ Object

Set the key-value pair, verifying existing CAS. Returns the resulting CAS value if succeeded, and falsy otherwise.



321
322
323
# File 'lib/dalli/client.rb', line 321

def set_cas(key, value, cas, ttl = nil, req_options = nil)
  perform(:set, key, value, ttl_or_default(ttl), cas, req_options)
end

#set_multi(hash, ttl = nil, req_options = nil) ⇒ void

This method returns an undefined value.

Set multiple keys and values efficiently using pipelining. This method is more efficient than calling set() in a loop because it batches requests by server and uses quiet mode.

Example:

client.set_multi({ 'key1' => 'value1', 'key2' => 'value2' }, 300)

Parameters:

  • key-value pairs to set

  • (defaults to: nil)

    time-to-live in seconds (optional, uses default if not provided)

  • (defaults to: nil)

    options passed to each set operation



310
311
312
313
314
315
316
# File 'lib/dalli/client.rb', line 310

def set_multi(hash, ttl = nil, req_options = nil)
  return if hash.empty?

  Instrumentation.trace('set_multi', multi_trace_attrs('set_multi', hash.size, hash.keys)) do
    pipelined_setter.process(hash, ttl_or_default(ttl), req_options)
  end
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’ => { … } }



445
446
447
448
449
450
451
452
# File 'lib/dalli/client.rb', line 445

def stats(type = nil)
  type = nil unless ALLOWED_STAT_KEYS.include? type
  values = {}
  ring.servers.each do |server|
    values[server.name.to_s] = 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.



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

def touch(key, ttl = nil)
  resp = perform(:touch, key, ttl_or_default(ttl))
  resp.nil? ? nil : true
end

#versionObject

Version of the memcache servers.



464
465
466
467
468
469
470
# File 'lib/dalli/client.rb', line 464

def version
  values = {}
  ring.servers.each do |server|
    values[server.name.to_s] = 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.

Yields:

  • (_self)

Yield Parameters:

  • _self (Dalli::Client)

    the object that the method was called on



498
499
500
# File 'lib/dalli/client.rb', line 498

def with
  yield self
end