module Cache

Constants

BRANCH_CACHE_BASE
CACHE_BASE_PATH
OBJECT_CACHE_BASE
REMOTES_REGEX

Public Instance Methods

branch_path(branch) click to toggle source
# File lib/cache.rb, line 84
def branch_path(branch)
  branch_name = branch.gsub(REMOTES_REGEX, "")
  File.join(".gitolemy", BRANCH_CACHE_BASE, "#{branch_name}.lock")
end
cache_path(key) click to toggle source
# File lib/cache.rb, line 14
def cache_path(key)
  File.join(CACHE_BASE_PATH, "#{key}.json.gz")
end
ensure_directory(dirname) click to toggle source
# File lib/cache.rb, line 67
def ensure_directory(dirname)
  return if File.directory?(dirname)
  FileUtils.makedirs(dirname)
end
index_commit(branch, commit_id) click to toggle source
# File lib/cache.rb, line 37
def index_commit(branch, commit_id)
  return if not persist?
  branch_path = branch_path(branch)
  ensure_directory(File.dirname(branch_path))
  FileUtils.touch(branch_path) if not File.exist?(branch_path)
  File.open(branch_path, "a") do |file|
    file << "#{commit_id.to_s}\n"
  end
end
index_commits(branch, commits) click to toggle source
# File lib/cache.rb, line 47
def index_commits(branch,  commits)
  return if not persist?
  File.write(branch_path(branch), commits.join("\n") + "\n")
end
last_indexed_commit(branch, commit_ids) click to toggle source
# File lib/cache.rb, line 52
def last_indexed_commit(branch, commit_ids)
  cached_commit_ids = File
    .read(branch_path(branch))
    .lines
    .map(&:chomp)
    .reduce({}) do |acc, commit_id|
      acc[commit_id.to_sym] = true
      acc
    end

  commit_ids.detect { |commit_id| cached_commit_ids[commit_id] }
rescue Errno::ENOENT
  nil
end
object_rel_path(object_id) click to toggle source
# File lib/cache.rb, line 80
def object_rel_path(object_id)
  File.join(OBJECT_CACHE_BASE, object_id)
end
persist?() click to toggle source
# File lib/cache.rb, line 89
def persist?
  ENV["GITOLEMY_PERSIST"] != "false"
end
read(key, default_val=nil) click to toggle source
# File lib/cache.rb, line 29
def read(key, default_val=nil)
  filename = cache_path(key)
  return default_val if not File.exist?(filename)
  JSON.parse(Zlib::GzipReader.open(filename) { |gz| gz.read })
rescue JSON::ParserError, Zlib::GzipFile::Error
  default_val
end
read_object(key, default_val=nil) click to toggle source
# File lib/cache.rb, line 72
def read_object(key, default_val=nil)
  read(object_rel_path(key.to_s), default_val)
end
write(key, data, existing_data=nil) click to toggle source
# File lib/cache.rb, line 18
def write(key, data, existing_data=nil)
  return data if not persist?
  filename = cache_path(key)
  dirname = File.dirname(filename)
  ensure_directory(dirname)

  store_data = existing_data.nil? ? data : existing_data.merge(data)
  Zlib::GzipWriter.open(filename) { |gz| gz.write(store_data.to_json) }
  data
end
write_object(key, data) click to toggle source
# File lib/cache.rb, line 76
def write_object(key, data)
  write(object_rel_path(key.to_s), data)
end