Class: Moneta::Stack

Inherits:
Base
  • Object
show all
Defined in:
lib/moneta/stack.rb

Overview

Combines multiple stores. Reads return the result from the first store, writes go to all stores.

Examples:

Add `Moneta::Stack` to proxy stack

Moneta.build do
  use(:Stack) do
    add { adapter :Redis }
    add { adapter :File, :dir => 'data' }
    add { adapter :File, :dir => 'replicate' }
  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) ⇒ Stack

Returns a new instance of Stack.


34
35
36
# File 'lib/moneta/stack.rb', line 34

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

Instance Attribute Details

#stackObject (readonly)


32
33
34
# File 'lib/moneta/stack.rb', line 32

def stack
  @stack
end

Instance Method Details

#clear(options = {}) ⇒ Object


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

def clear(options = {})
  @stack.each {|s| s.clear }
  self
end

#closeObject


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

def close
  @stack.each {|s| s.close }
  nil
end

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


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

def delete(key, options = {})
  @stack.inject(nil) do |value, s|
    v = s.delete(key, options)
    value || v
  end
end

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


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

def increment(key, amount = 1, options = {})
  last = nil
  @stack.each {|s| last = s.increment(key, amount, options) }
  last
end

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

Returns:

  • (Boolean)

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

def key?(key, options = {})
  @stack.any? {|s| s.key?(key) }
end

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


42
43
44
45
46
47
48
# File 'lib/moneta/stack.rb', line 42

def load(key, options = {})
  @stack.each do |s|
    value = s.load(key, options)
    return value if value
  end
  nil
end

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


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

def store(key, value, options = {})
  @stack.each {|s| s.store(key, value, options) }
  value
end