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.

Examples:

Add ‘Moneta::Cache` to proxy stack

Moneta.build do
  use(:Cache) do
   backend { 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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}, &block) ⇒ Cache

Returns a new instance of Cache.



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

def initialize(options = {}, &block)
  @cache, @backend = DSL.new(options, &block).result
end

Instance Attribute Details

#backendObject (readonly)



42
43
44
# File 'lib/moneta/cache.rb', line 42

def backend
  @backend
end

#cacheObject (readonly)



42
43
44
# File 'lib/moneta/cache.rb', line 42

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: {})


82
83
84
85
86
# File 'lib/moneta/cache.rb', line 82

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

#closeObject

Explicitly close the store

Returns:

  • nil



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

def close
  @cache.close
  @backend.close
end

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

Delete the key from the store and return the current value

Parameters:

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

Returns:

  • (Object)

    current value



76
77
78
79
# File 'lib/moneta/cache.rb', line 76

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

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

Atomically increment integer value with key

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

This method also accepts negative amounts.

Parameters:

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

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)
  @backend.increment(key, amount, options)
end

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

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


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

def key?(key, options = {})
  @cache.key?(key, options) || @backend.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: {})

Returns:

  • (Object)

    value



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

def load(key, options = {})
  value = @cache.load(key, options)
  if value == nil
    value = @backend.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: {})

Returns:

  • value



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

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