class Prez::Cache

Constants

DIR

Attributes

toc[R]

Public Class Methods

get(key, pre_cache_contents) { || ... } click to toggle source
# File lib/prez/cache.rb, line 73
def get(key, pre_cache_contents)
  hash = md5 pre_cache_contents

  if instance.include?(key, hash)
    instance.get key, hash
  else
    yield.tap do |results|
      instance.put key, hash, results
    end
  end
new() click to toggle source
# File lib/prez/cache.rb, line 11
def initialize
  unless File.directory?(dir)
    Dir.mkdir dir
  end

  if File.exists?(toc_file)
    @toc = YAML.load File.read(toc_file)
  else
    @toc = { prez_version: Prez::Version.to_s }
  end
end

Public Instance Methods

cached_path(key, hash) click to toggle source
# File lib/prez/cache.rb, line 35
def cached_path(key, hash)
  File.join dir, Prez::Cache.md5(key), hash
end
dir() click to toggle source
# File lib/prez/cache.rb, line 23
def dir
  Prez::Cache::DIR
end
get(key, hash) click to toggle source
# File lib/prez/cache.rb, line 39
def get(key, hash)
  File.read cached_path(key, hash)
ensure
  toc[key][:last_touched][hash] = Time.now
  save
end
include?(key, hash) click to toggle source
# File lib/prez/cache.rb, line 31
def include?(key, hash)
  toc.include?(key) && File.file?(cached_path(key, hash))
end
instance() click to toggle source
# File lib/prez/cache.rb, line 89
def instance
  @instance ||= Prez::Cache.new
end
md5(contents) click to toggle source
# File lib/prez/cache.rb, line 85
def md5(contents)
  Digest::MD5.hexdigest contents
end
put(key, hash, contents) click to toggle source
# File lib/prez/cache.rb, line 46
def put(key, hash, contents)
  unless include?(key, hash)
    toc[key] = {
      last_touched: {},
      saved: {}
    }
  end

  path = cached_path key, hash

  unless File.directory?(File.dirname(path))
    FileUtils.makedirs File.dirname(path)
  end

  File.write path, contents
  toc[key][:key_hash] = Prez::Cache.md5 key
  toc[key][:last_touched][hash] = Time.now
  toc[key][:saved][hash] = Time.now
ensure
  save
end
save() click to toggle source
# File lib/prez/cache.rb, line 68
def save
  File.write toc_file, YAML.dump(toc)
end
toc_file() click to toggle source
# File lib/prez/cache.rb, line 27
def toc_file
  File.join dir, "toc.yml"
end