Class: APICache::Cache

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

Overview

Cache performs calculations relating to the status of items stored in the cache and delegates storage to the various cache stores.

Instance Method Summary (collapse)

Constructor Details

- (Cache) initialize(key, options)

Takes the following options

cache

Length of time to cache before re-requesting

valid

Length of time to consider data still valid if API cannot be fetched - :forever is a valid option.



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

def initialize(key, options)
  @key = key
  @cache = options[:cache]
  @valid = options[:valid]
end

Instance Method Details

- (Object) delete



50
51
52
# File 'lib/api_cache/cache.rb', line 50

def delete
  store.delete(hash)
end

- (Object) get



41
42
43
# File 'lib/api_cache/cache.rb', line 41

def get
  store.get(hash)
end

- (Object) set(value)



45
46
47
48
# File 'lib/api_cache/cache.rb', line 45

def set(value)
  store.set(hash, value)
  true
end

- (Object) state

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)



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api_cache/cache.rb', line 27

def state
  if store.exists?(hash)
    if !store.expired?(hash, @cache)
      :current
    elsif (@valid == :forever) || !store.expired?(hash, @valid)
      :refetch
    else
      :invalid
    end
  else
    :missing
  end
end