Class: Moneta::Adapters::MemcachedDalli

Inherits:
Moneta::Adapter show all
Includes:
ExpiresSupport
Defined in:
lib/moneta/adapters/memcached/dalli.rb

Overview

Memcached backend (using gem dalli)

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from ExpiresSupport

included

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #key?, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :server (String) — default: '127.0.0.1:11211'

    Memcached server

  • :expires (Integer)

    Default expiration time

  • :backend (::Dalli::Client)

    Use existing backend instance

  • Other (Object)

    options passed to ‘Dalli::Client#new`



18
# File 'lib/moneta/adapters/memcached/dalli.rb', line 18

backend { |server: '127.0.0.1:11211', **options| ::Dalli::Client.new(server, options) }

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


61
62
63
64
# File 'lib/moneta/adapters/memcached/dalli.rb', line 61

def clear(options = {})
  backend.flush_all
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



72
73
74
75
# File 'lib/moneta/adapters/memcached/dalli.rb', line 72

def close
  backend.close
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



67
68
69
# File 'lib/moneta/adapters/memcached/dalli.rb', line 67

def create(key, value, options = {})
  !!backend.add(key, value, expires_value(options) || nil, raw: true)
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



37
38
39
40
41
# File 'lib/moneta/adapters/memcached/dalli.rb', line 37

def delete(key, options = {})
  value = backend.get(key)
  backend.delete(key)
  value
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/moneta/adapters/memcached/dalli.rb', line 44

def increment(key, amount = 1, options = {})
  result =
    if amount >= 0
      backend.incr(key, amount, expires_value(options) || nil)
    else
      backend.decr(key, -amount, expires_value(options) || nil)
    end
  if result
    result
  elsif create(key, amount.to_s, options)
    amount
  else
    increment(key, amount, options)
  end
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



21
22
23
24
25
26
27
28
# File 'lib/moneta/adapters/memcached/dalli.rb', line 21

def load(key, options = {})
  value = backend.get(key)
  if value
    expires = expires_value(options, nil)
    backend.set(key, value, expires || nil, raw: true) if expires != nil
    value
  end
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses Defaults#key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/moneta/adapters/memcached/dalli.rb', line 100

def merge!(pairs, options = {})
  expires = expires_value(options)
  expires = expires.to_i if Numeric === expires
  expires ||= nil

  if block_given?
    keys = pairs.map { |key, _| key }.to_a
    old_pairs = backend.get_multi(keys)
    pairs = pairs.map do |key, new_value|
      if old_pairs.key? key
        new_value = yield(key, old_pairs[key], new_value)
      end
      [key, new_value]
    end
  end

  backend.multi do
    pairs.each do |key, value|
      backend.set(key, value, expires, raw: true)
    end
  end

  self
end

#slice(*keys, **options) ⇒ <(Object, Object)>

Note:

The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (<(Object, Object)>)

    A collection of key-value pairs



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/moneta/adapters/memcached/dalli.rb', line 78

def slice(*keys, **options)
  backend.get_multi(keys).tap do |pairs|
    next if pairs.empty?
    expires = expires_value(options, nil)
    next if expires == nil
    expires = expires.to_i if Numeric === expires
    expires ||= 0
    backend.multi do
      pairs.each do |key, value|
        backend.set(key, value, expires, false)
      end
    end
  end
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



31
32
33
34
# File 'lib/moneta/adapters/memcached/dalli.rb', line 31

def store(key, value, options = {})
  backend.set(key, value, expires_value(options) || nil, raw: true)
  value
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



94
95
96
97
# File 'lib/moneta/adapters/memcached/dalli.rb', line 94

def values_at(*keys, **options)
  pairs = slice(*keys, **options)
  keys.map { |key| pairs.delete(key) }
end