Class: Geocoder::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/geocoder/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(store, config) ⇒ Cache

Returns a new instance of Cache.


6
7
8
9
# File 'lib/geocoder/cache.rb', line 6

def initialize(store, config)
  @class = (Object.const_get("Geocoder::CacheStore::#{store.class}") rescue Geocoder::CacheStore::Generic)
  @store_service = @class.new(store, config)
end

Instance Method Details

#[](url) ⇒ Object

Read from the Cache.


14
15
16
17
18
# File 'lib/geocoder/cache.rb', line 14

def [](url)
  interpret store_service.read(url)
rescue => e
  Geocoder.log(:warn, "Geocoder cache read error: #{e}")
end

#[]=(url, value) ⇒ Object

Write to the Cache.


23
24
25
26
27
# File 'lib/geocoder/cache.rb', line 23

def []=(url, value)
  store_service.write(url, value)
rescue => e
  Geocoder.log(:warn, "Geocoder cache write error: #{e}")
end

#expire(url) ⇒ Object

Delete cache entry for given URL, or pass :all to clear all URLs.


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/geocoder/cache.rb', line 33

def expire(url)
  if url == :all
    if store_service.respond_to?(:keys)
      urls.each{ |u| expire(u) }
    else
      raise(NoMethodError, "The Geocoder cache store must implement `#keys` for `expire(:all)` to work")
    end
  else
    expire_single_url(url)
  end
end