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, #each_key, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at

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


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

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

#closeObject



92
93
94
95
# File 'lib/moneta/stack.rb', line 92

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

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



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

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

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

Delete the key from the store and return the current value

Parameters:

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

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



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

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

#featuresObject



98
99
100
101
102
103
104
# File 'lib/moneta/stack.rb', line 98

def features
  @features ||=
    begin
      features = @stack.map(&:features)
      (features.inject(features.first, &:&) - [:each_key]).freeze
    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: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

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

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

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