File: cache_bypass.rb

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,000 bytes parent folder | download
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
# 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