Class: APICache::Cache
- Inherits:
-
Object
- Object
- APICache::Cache
- 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
- #delete ⇒ Object
- #get ⇒ Object
-
#initialize(key, options) ⇒ Cache
constructor
Takes the following options.
- #set(value) ⇒ Object
-
#state ⇒ Object
Returns one of the following options depending on the state of the key:.
Constructor Details
#initialize(key, options) ⇒ Cache
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, ) @key = key @cache = [:cache] @valid = [:valid] end |
Instance Method Details
#delete ⇒ Object
50 51 52 53 |
# File 'lib/api_cache/cache.rb', line 50 def delete store.delete(hash) return nil end |
#get ⇒ Object
41 42 43 |
# File 'lib/api_cache/cache.rb', line 41 def get store.get(hash) end |
#set(value) ⇒ Object
45 46 47 48 |
# File 'lib/api_cache/cache.rb', line 45 def set(value) store.set(hash, value) true end |
#state ⇒ Object
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 |