1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
module Moneta
# Provides a fallback to a second store when an exception is raised
#
# @example Basic usage - catches any {IOError} and falls back to {Moneta::Adapters:Null}
# Moneta.build do
# use :Fallback
# adapter :Client
# end
#
# @example Specifying an exception to rescue
# Moneta.build do
# use :Fallback, rescue: Redis::CannotConnectError
# adapter :Redis
# end
#
# @example Specifying a different fallback
# Moneta.build do
# use :Fallback do
# # This is a new builder context
# adapter :Memory
# end
# adapter :File, dir: 'cache'
# end
#
# @api public
class Fallback < Wrapper
# @param [Moneta store] adapter The underlying store
# @param [Hash] options
# @option options [Moneta store] :fallback (:Null store) The store to fall
# back on
# @option options [Class|Array<Class>] :rescue ([IOError]) The list
# of exceptions that should be rescued
# @yieldreturn [Moneta store] Moneta store built using the builder API
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
protected
def wrap(name, *args, &block)
yield
rescue => e
raise unless @rescue.any? { |rescuable| rescuable === e }
fallback(name, *args, &block)
end
def fallback(name, *args, &block)
result =
case name
when :values_at, :fetch_values, :slice
keys, options = args
@fallback.public_send(name, *keys, **options, &block)
else
@fallback.public_send(name, *args, &block)
end
# Don't expose the fallback class to the caller
if result == @fallback
self
else
result
end
end
end
end
|