Class: Slimmer::Cache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/slimmer/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

TODO: use a real cache rather than an in memory hash



9
10
11
12
13
14
15
16
# File 'lib/slimmer/cache.rb', line 9

def initialize
  @cache_last_reset= Time.now

  @use_cache = false
  @cache_ttl = (5 * 60) # 5 mins

  @data_store = {}
end

Instance Attribute Details

#cache_ttl=(value) ⇒ Object (writeonly)

Sets the attribute cache_ttl

Parameters:

  • value

    the value to set the attribute cache_ttl to.



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

def cache_ttl=(value)
  @cache_ttl = value
end

#use_cache=(value) ⇒ Object (writeonly)

Sets the attribute use_cache

Parameters:

  • value

    the value to set the attribute use_cache to.



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

def use_cache=(value)
  @use_cache = value
end

Instance Method Details

#clearObject



18
19
20
# File 'lib/slimmer/cache.rb', line 18

def clear
  @data_store.clear
end

#fetch(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/slimmer/cache.rb', line 22

def fetch(key)
  clear_cache_if_stale
  data = @data_store[key]

  if data.nil?
    data = yield
  end

  if @use_cache
    @data_store[key] = data
  end

  data
end