Class: Slimmer::Cache
- Inherits:
-
Object
- Object
- Slimmer::Cache
- Includes:
- Singleton
- Defined in:
- lib/slimmer/cache.rb
Instance Attribute Summary collapse
-
#cache_ttl ⇒ Object
writeonly
Sets the attribute cache_ttl.
-
#use_cache ⇒ Object
writeonly
Sets the attribute use_cache.
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize ⇒ Cache
constructor
TODO: use a real cache rather than an in memory hash.
Constructor Details
#initialize ⇒ Cache
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
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
6 7 8 |
# File 'lib/slimmer/cache.rb', line 6 def use_cache=(value) @use_cache = value end |
Instance Method Details
#clear ⇒ Object
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 |