Class: Schlib::Cache
- Inherits:
-
Object
- Object
- Schlib::Cache
- Defined in:
- lib/schlib/cache.rb
Instance Method Summary collapse
- #cache(cache_key) ⇒ Object
- #clear ⇒ Object
- #data ⇒ Object
-
#initialize(cache_file = '/tmp/schlib_cache.tmp') ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(cache_file = '/tmp/schlib_cache.tmp') ⇒ Cache
Returns a new instance of Cache.
6 7 8 |
# File 'lib/schlib/cache.rb', line 6 def initialize(cache_file = '/tmp/schlib_cache.tmp') @cache_file = cache_file end |
Instance Method Details
#cache(cache_key) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/schlib/cache.rb', line 10 def cache(cache_key) mutable_cache_data = data thing = mutable_cache_data[cache_key.to_s] ||= yield File.write cache_file, JSON.dump(mutable_cache_data) thing end |
#clear ⇒ Object
22 23 24 |
# File 'lib/schlib/cache.rb', line 22 def clear File.delete cache_file end |
#data ⇒ Object
17 18 19 20 |
# File 'lib/schlib/cache.rb', line 17 def data File.write cache_file, JSON.dump({}) unless File.exist? cache_file JSON.parse File.read cache_file end |