From 34787a58d35d2605a85556c6eeca38f0c16b8f70 Mon Sep 17 00:00:00 2001
From: Pascal Betz <pascal.betz@simplificator.com>
Date: Wed, 1 May 2013 18:50:59 +0200
Subject: [PATCH] + Example on how to bypass cache

---
 examples/cache_bypass.rb | 48 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 examples/cache_bypass.rb

diff --git a/examples/cache_bypass.rb b/examples/cache_bypass.rb
new file mode 100644
index 00000000..29668744
--- /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
-- 
GitLab