Class: Moneta::Adapters::ActiveSupportCache

Inherits:
Object
  • Object
show all
Includes:
Defaults, ExpiresSupport
Defined in:
lib/moneta/adapters/activesupportcache.rb

Overview

ActiveSupport::Cache::Store adapter

Instance Attribute Summary

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ ActiveSupportCache

Returns a new instance of ActiveSupportCache

Options Hash (options):

  • :expires (Numeric)

    default expiration in seconds


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/moneta/adapters/activesupportcache.rb', line 13

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  @backend =
    if options[:backend]
      options[:backend]
    elsif defined?(Rails)
      Rails.cache
    else
      raise ArgumentError, 'Option :backend is required'
    end
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store


80
81
82
83
# File 'lib/moneta/adapters/activesupportcache.rb', line 80

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

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

Delete the key from the store and return the current value

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


71
72
73
74
75
76
77
# File 'lib/moneta/adapters/activesupportcache.rb', line 71

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.

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/moneta/adapters/activesupportcache.rb', line 57

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

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


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

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

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


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/moneta/adapters/activesupportcache.rb', line 36

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
  if options[:raw]
    value && value.to_s
  else
    value
  end
end

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

Store value with key

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


50
51
52
53
54
# File 'lib/moneta/adapters/activesupportcache.rb', line 50

def store(key, value, options = {})
  expires = expires_value(options)
  @backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
  value
end