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 = {}) {|Builder| ... } ⇒ Stack

Returns a new instance of Stack.

Parameters:

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

    Options hash

Options Hash (options):

  • :stack (Array)

    Array of Moneta stores

Yield Parameters:

  • Builder

    block



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

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

Instance Attribute Details

#stackObject (readonly)



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

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


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

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

#closeObject



85
86
87
88
# File 'lib/moneta/stack.rb', line 85

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



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

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



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

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)


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

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



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

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



58
59
60
61
# File 'lib/moneta/stack.rb', line 58

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