Class: Moneta::Fallback

Inherits:
Wrapper show all
Defined in:
lib/moneta/fallback.rb

Overview

Provides a fallback to a second store when an exception is raised

Examples:

Basic usage - catches any IOError and falls back to Adapters:Null

Moneta.build do
  use :Fallback
  adapter :Client
end

Specifying an exception to rescue

Moneta.build do
  use :Fallback, rescue: Redis::CannotConnectError
  adapter :Redis
end

Specifying a different fallback

Moneta.build do
  use :Fallback do
    # This is a new builder context
    adapter :Memory
  end
  adapter :File, dir: 'cache'
end

Instance Attribute Summary

Attributes inherited from Proxy

#adapter

Instance Method Summary collapse

Methods inherited from Wrapper

#clear, #close, #create, #delete, #each_key, #features, #fetch_values, #increment, #key?, #load, #merge!, #slice, #store, #values_at

Methods inherited from Proxy

#clear, #close, #create, #delete, #each_key, #features, features_mask, #fetch_values, #increment, #key?, #load, #merge!, not_supports, #slice, #store, #values_at

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

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

Returns a new instance of Fallback.

Parameters:

  • adapter (Moneta store)

    The underlying store

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

Options Hash (options):

  • :fallback (Moneta store) — default: :Null store

    The store to fall back on

  • :rescue (Class|Array<Class>) — default: [IOError]

    The list of exceptions that should be rescued

Yield Returns:

  • (Moneta store)

    Moneta store built using the builder API



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/moneta/fallback.rb', line 34

def initialize(adapter, options = {}, &block)
  super

  @fallback =
    if block_given?
      ::Moneta.build(&block)
    elsif options.key?(:fallback)
      options.delete(:fallback)
    else
      ::Moneta::Adapters::Null.new
    end

  @rescue =
    case options[:rescue]
    when nil
      [::IOError]
    when Array
      options[:rescue]
    else
      [options[:rescue]]
    end
end