Class: APICache::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Cache



6
7
8
# File 'lib/api_cache/cache.rb', line 6

def initialize(store)
  @store = store.send(:new)
end

Instance Attribute Details

#storeObject

Returns the value of attribute store.



5
6
7
# File 'lib/api_cache/cache.rb', line 5

def store
  @store
end

Instance Method Details

#encode(key) ⇒ Object



38
39
40
# File 'lib/api_cache/cache.rb', line 38

def encode(key)
  Digest::MD5.hexdigest key
end

#get(key) ⇒ Object



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

def get(key)
  @store.get(encode(key))
end

#set(key, value) ⇒ Object



34
35
36
37
# File 'lib/api_cache/cache.rb', line 34

def set(key, value)
  @store.set(encode(key), value)
  true
end

#state(key, refetch_time, invalid_time) ⇒ 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)



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/api_cache/cache.rb', line 16

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