Sha256: 962be9fd5a657a3eee5b3b97cde4a1ee0b3c8a077ef98550b5ea28fe0d1448a9

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# Cache performs calculations relating to the status of items stored in the
# cache and delegates storage to the various cache stores.
require 'digest/md5'
class APICache::Cache
  attr_accessor :store
  def initialize(store)
    @store = store.send(:new)
  end
  
  # Returns one of the following options depending on the state of the key:
  # 
  # * :current (key has been set recently)
  # * :refetch (data should be refetched but is still available for use)
  # * :invalid (data is too old to be useful)
  # * :missing (do data for this key)
  def state(key, refetch_time, invalid_time)
    if @store.exists?(encode(key))
      if !@store.expired?(encode(key), refetch_time)
        :current
      elsif (invalid_time == :forever) || !@store.expired?(encode(key), invalid_time)
        :refetch
      else
        :invalid
      end
    else
      :missing
    end
  end
  
  def get(key)
    @store.get(encode(key))
  end
  
  def set(key, value)
    @store.set(encode(key), value)
    true
  end
  def encode(key)
    Digest::MD5.hexdigest key
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mloughran-api_cache-0.1.1 lib/api_cache/cache.rb
mloughran-api_cache-0.1.2 lib/api_cache/cache.rb
api_cache-0.1.2 lib/api_cache/cache.rb