Class: Moneta::Cache

Inherits:
Base
  • Object
show all
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 inherited from Base

#[], #[]=, #decrement, #fetch

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Constructor Details

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

Returns a new instance of Cache.



40
41
42
# File 'lib/moneta/cache.rb', line 40

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

Instance Attribute Details

#backendObject (readonly)



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

def backend
  @backend
end

#cacheObject (readonly)



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

def cache
  @cache
end

Instance Method Details

#clear(options = {}) ⇒ Object



73
74
75
76
77
# File 'lib/moneta/cache.rb', line 73

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

#closeObject



79
80
81
82
# File 'lib/moneta/cache.rb', line 79

def close
  @cache.close
  @backend.close
end

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



68
69
70
71
# File 'lib/moneta/cache.rb', line 68

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

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



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

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

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

Returns:

  • (Boolean)


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

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

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



48
49
50
51
52
53
54
55
# File 'lib/moneta/cache.rb', line 48

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

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



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

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