Class: Moneta::Adapters::ActiveSupportCache

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

Overview

ActiveSupport::Cache::Store adapter

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

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Object

Parameters:

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

Options Hash (options):

  • :backend (ActiveSupport::Cache::Store) — default: Rails.cache

    Cache store to use

  • :expires (Numeric)

    default expiration in seconds



14
# File 'lib/moneta/adapters/activesupportcache.rb', line 14

backend { Rails.cache if defined?(Rails) }

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


67
68
69
70
# File 'lib/moneta/adapters/activesupportcache.rb', line 67

def clear(options = {})
  backend.clear
  self
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



58
59
60
61
62
63
64
# File 'lib/moneta/adapters/activesupportcache.rb', line 58

def delete(key, options = {})
  value = backend.read(key, options)
  if value != nil
    backend.delete(key, options)
    options[:raw] ? value.to_s : value
  end
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
# File 'lib/moneta/adapters/activesupportcache.rb', line 44

def increment(key, amount = 1, options = {})
  expires = expires_value(options)
  options.delete(:raw)
  existing = Integer(backend.fetch(key, options.merge(raw: true)) { 0 })
  if amount > 0
    backend.increment(key, amount, options.merge(expires_in: expires ? expires.seconds : nil))
  elsif amount < 0
    backend.decrement(key, -amount, options.merge(expires_in: expires ? expires.seconds : nil))
  else
    existing
  end
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/moneta/adapters/activesupportcache.rb', line 17

def key?(key, options = {})
  backend.exist?(key).tap do |exists|
    if exists && (expires = expires_value(options, nil)) != nil
      value = backend.read(key, options)
      backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
    end
  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



27
28
29
30
31
32
33
34
# File 'lib/moneta/adapters/activesupportcache.rb', line 27

def load(key, options = {})
  expires = expires_value(options, nil)
  value = backend.read(key, options)
  if value and expires != nil
    backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
  end
  value
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 #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)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/moneta/adapters/activesupportcache.rb', line 95

def merge!(pairs, options = {})
  if block_given?
    existing = slice(*pairs.map { |k, _| k }, **options)
    pairs = pairs.map do |key, new_value|
      if existing.key?(key)
        new_value = yield(key, existing[key], new_value)
      end

      [key, new_value]
    end
  end

  hash = Hash === pairs ? pairs : Hash[pairs.to_a]
  expires = expires_value(options)
  backend.write_multi(hash, options.merge(expires_in: expires ? expires.seconds : nil))
  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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/moneta/adapters/activesupportcache.rb', line 73

def slice(*keys, **options)
  hash = backend.read_multi(*keys)
  if (expires = expires_value(options, nil)) != nil
    hash.each do |key, value|
      backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
    end
  end
  if options[:raw]
    hash.each do |key, value|
      hash[key] = value.to_s if value != nil
    end
  end
  hash
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



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

def store(key, value, options = {})
  expires = expires_value(options)
  backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
  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



89
90
91
92
# File 'lib/moneta/adapters/activesupportcache.rb', line 89

def values_at(*keys, **options)
  hash = slice(*keys, **options)
  keys.map { |key| hash[key] }
end