Class: Moneta::Stack

Inherits:
Object
  • Object
show all
Includes:
Defaults
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 included from Defaults

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

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

Returns a new instance of Stack.



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

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

Instance Attribute Details

#stackObject (readonly)



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

def stack
  @stack
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


77
78
79
80
# File 'lib/moneta/stack.rb', line 77

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

#closeObject



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

def close
  @stack.each {|s| s.close }
  nil
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



69
70
71
72
73
74
# File 'lib/moneta/stack.rb', line 69

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)


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

def key?(key, options = {})
  @stack.any? {|s| s.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



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

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

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

Store value with key

Parameters:

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

Returns:

  • value



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

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