class GraphQL::Language::Cache

Constants

DIGEST

Public Class Methods

new(path) click to toggle source
# File lib/graphql/language/cache.rb, line 9
def initialize(path)
  @path = path
end

Public Instance Methods

fetch(filename) { || ... } click to toggle source
# File lib/graphql/language/cache.rb, line 14
def fetch(filename)
  hash = DIGEST.dup << filename
  begin
    hash << File.mtime(filename).to_i.to_s
  rescue SystemCallError
    return yield
  end
  cache_path = @path.join(hash.to_s)

  if cache_path.exist?
    Marshal.load(cache_path.read)
  else
    payload = yield
    tmp_path = "#{cache_path}.#{rand}"

    @path.mkpath
    File.binwrite(tmp_path, Marshal.dump(payload))
    File.rename(tmp_path, cache_path.to_s)
    payload
  end
end