Class: EarthTools::Cache

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

Overview

The Earth Tools cache wrapper.

Must respond to: -> [] – read -> []= – write -> del – delete -> keys – list of keys

Instance Method Summary collapse

Constructor Details

#initialize(store, prefix) ⇒ Cache

Returns a new instance of Cache.



16
17
18
19
# File 'lib/earth_tools/cache.rb', line 16

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

Instance Method Details

#[](url) ⇒ Object

Read from the cache.

Returns:

  • the object saved in the cache



24
25
26
# File 'lib/earth_tools/cache.rb', line 24

def [](url)
  @store[key_for(url)]
end

#[]=(url, value) ⇒ Object

Write to the cache.



30
31
32
# File 'lib/earth_tools/cache.rb', line 30

def []=(url, value)
  @store[key_for(url)] = value
end

#expire(url) ⇒ Object

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



37
38
39
40
41
42
43
# File 'lib/earth_tools/cache.rb', line 37

def expire(url)
  if url == :all
    urls.each{ |u| expire(u) }
  else
    @store.send(@store.respond_to?(:del) ? :del : :delete, key_for(url))
  end
end