class EarthTools::Cache

The Earth Tools cache wrapper.

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

Attributes

prefix[R]
store[R]

Public Class Methods

new(store, prefix) click to toggle source

@constructor

# File lib/earth_tools/cache.rb, line 16
def initialize(store, prefix)
  @store = store
  @prefix = prefix
end

Public Instance Methods

[](url) click to toggle source

Read from the cache. @return the object saved in the cache

# File lib/earth_tools/cache.rb, line 24
def [](url)
  @store[key_for(url)]
end
[]=(url, value) click to toggle source

Write to the cache.

# File lib/earth_tools/cache.rb, line 30
def []=(url, value)
  @store[key_for(url)] = value
end
expire(url) click to toggle source

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

# 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

Private Instance Methods

cleave(value) click to toggle source

Convert empty string to nil. (Some key/value stores return empty string instead of nil.) @return [Object, nil] the object or nil

# File lib/earth_tools/cache.rb, line 75
def cleave(value)
  value == "" ? nil : value
end
key_for(url) click to toggle source

Cache key for a given URL. @return [String] the cache key

# File lib/earth_tools/cache.rb, line 52
def key_for(url)
  [@prefix, url].join
end
keys() click to toggle source

Array of keys with the currently configured prefix that have non-nil values. @return [Array] the list of keys

# File lib/earth_tools/cache.rb, line 60
def keys
  @store.keys.select{ |k| k.match /^#{@prefix}/ and cleave(@store[k]) }
end
urls() click to toggle source

Array of cached URLs. @return [Array] the list of cached URLs

# File lib/earth_tools/cache.rb, line 67
def urls
  keys.map{ |k| k[/^#{@prefix}(.*)/, 1] }
end