Class: Schlib::Cache

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

Instance Method Summary collapse

Constructor Details

#initialize(cache_file = '/tmp/schlib_cache.tmp') ⇒ 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] ||= yield
  File.write cache_file, JSON.dump(mutable_cache)
  thing
end

#dataObject



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

def data
  File.write cache_file, Marshal.dump({}) unless File.exist? cache_file
  JSON.parse File.read cache_file
end