Class: Geocoder::Cache

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

Instance Method Summary collapse

Constructor Details

#initialize(store, prefix) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/geocoder/cache.rb', line 4

def initialize(store, prefix)
  @store = store
  @prefix = prefix
end

Instance Method Details

#[](url) ⇒ Object

Read from the Cache.



12
13
14
15
16
17
18
19
20
21
# File 'lib/geocoder/cache.rb', line 12

def [](url)
  interpret case
    when store.respond_to?(:[])
      store[key_for(url)]
    when store.respond_to?(:get)
      store.get key_for(url)
    when store.respond_to?(:read)
      store.read key_for(url)
  end
end

#[]=(url, value) ⇒ Object

Write to the Cache.



26
27
28
29
30
31
32
33
34
35
# File 'lib/geocoder/cache.rb', line 26

def []=(url, value)
  case
    when store.respond_to?(:[]=)
      store[key_for(url)] = value
    when store.respond_to?(:set)
      store.set key_for(url), value
    when store.respond_to?(:write)
      store.write key_for(url), value
  end
end

#expire(url) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/geocoder/cache.rb', line 41

def expire(url)
  if url == :all
    if store.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