diff --git a/examples/cache_bypass.rb b/examples/cache_bypass.rb new file mode 100644 index 0000000000000000000000000000000000000000..888ac34568cddf08730680eac0a5e107630dc960 --- /dev/null +++ b/examples/cache_bypass.rb @@ -0,0 +1,48 @@ +# This class allows you to configure how Geocoder should treat errors that occur when +# the cache is not available. +# Configure it like this +# config/initializers/geocoder.rb +# Geocoder.configure( +# :cache => Geocoder::CacheBypass.new(Redis.new) +# ) +# +# Depending on the value of @bypass this will either +# raise the exception (true) or swallow it and pretend the cache did not return a hit (false) +# +class Geocoder::CacheBypass + def initialize(target, bypass = true) + @target = target + @bypass = bypass + end + + + def [](key) + with_bypass { @target[key] } + end + + def []=(key, value) + with_bypass(value) { @target[key] = value } + end + + def keys + with_bypass([]) { @target.keys } + end + + def del(key) + with_bypass { @target.del(key) } + end + + private + + def with_bypass(return_value_if_exception = nil, &block) + begin + yield + rescue + if @bypass + return_value_if_exception + else + raise # reraise original exception + end + end + end +end \ No newline at end of file