Class: Moneta::Cache

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/cache.rb

Overview

Combines two stores. One is used as cache, the other as backend adapter.

Examples:

Add ‘Moneta::Cache` to proxy stack

Moneta.build do
  use(:Cache) do
   adapter { adapter :File, dir: 'data' }
   cache { adapter :Memory }
  end
end

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) {|Builder| ... } ⇒ Cache

Returns a new instance of Cache.

Parameters:

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

    Options hash

Options Hash (options):

  • :cache (Moneta store)

    Moneta store used as cache

  • :adapter (Moneta store)

    Moneta store used as adapter

Yield Parameters:

  • Builder

    block



44
45
46
47
# File 'lib/moneta/cache.rb', line 44

def initialize(options = {}, &block)
  @cache, @adapter = options[:cache], options[:adapter]
  DSL.new(self, &block) if block_given?
end

Instance Attribute Details

#adapterObject



38
39
40
# File 'lib/moneta/cache.rb', line 38

def adapter
  @adapter
end

#cacheObject



38
39
40
# File 'lib/moneta/cache.rb', line 38

def cache
  @cache
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


92
93
94
95
96
# File 'lib/moneta/cache.rb', line 92

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

#closeObject

Explicitly close the store

Returns:

  • nil



99
100
101
102
# File 'lib/moneta/cache.rb', line 99

def close
  @cache.close
  @adapter.close
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



76
77
78
79
80
81
82
83
# File 'lib/moneta/cache.rb', line 76

def create(key, value, options = {})
  if @adapter.create(key, value, options)
    @cache.store(key, value, options)
    true
  else
    false
  end
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



86
87
88
89
# File 'lib/moneta/cache.rb', line 86

def delete(key, options = {})
  @cache.delete(key, options)
  @adapter.delete(key, options)
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

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

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


105
106
107
108
109
110
111
112
# File 'lib/moneta/cache.rb', line 105

def each_key(&block)
  raise NotImplementedError, 'adapter doesn\'t support #each_key' \
    unless supports? :each_key

  return enum_for(:each_key) unless block_given?
  @adapter.each_key(&block)
  self
end

#featuresArray<Symbol>

Returns features list

Returns:

  • (Array<Symbol>)

    list of features



115
116
117
# File 'lib/moneta/cache.rb', line 115

def features
  @features ||= ((@cache.features + [:create, :increment, :each_key]) & @adapter.features).freeze
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



70
71
72
73
# File 'lib/moneta/cache.rb', line 70

def increment(key, amount = 1, options = {})
  @cache.delete(key, options)
  @adapter.increment(key, amount, options)
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)


50
51
52
# File 'lib/moneta/cache.rb', line 50

def key?(key, options = {})
  @cache.key?(key, options) || @adapter.key?(key, options)
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 (Moneta::Cache reloads from adapter, Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



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

def load(key, options = {})
  if options[:sync] || (value = @cache.load(key, options)) == nil
    value = @adapter.load(key, options)
    @cache.store(key, value, options) if value != nil
  end
  value
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



64
65
66
67
# File 'lib/moneta/cache.rb', line 64

def store(key, value, options = {})
  @cache.store(key, value, options)
  @adapter.store(key, value, options)
end