class Schlib::Cache

Attributes

cache_file[R]

Public Class Methods

new(cache_file = '/tmp/schlib_cache.tmp') click to toggle source
# File lib/schlib/cache.rb, line 7
def initialize(cache_file = '/tmp/schlib_cache.tmp')
  @cache_file = cache_file
end

Public Instance Methods

cache(cache_key) { || ... } click to toggle source
# File lib/schlib/cache.rb, line 11
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() click to toggle source
# File lib/schlib/cache.rb, line 29
def clear
  File.delete cache_file
end
data() click to toggle source
# File lib/schlib/cache.rb, line 18
def data
  File.write cache_file, JSON.dump({}) unless File.exist? cache_file
  JSON.parse File.read cache_file
end
reset(key) click to toggle source
# File lib/schlib/cache.rb, line 23
def reset(key)
  mutable_cache_data = data
  mutable_cache_data[key.to_s] = nil
  File.write cache_file, JSON.dump(mutable_cache_data)
end