add extension points for GitAccess caching
This commit is contained in:
+31
-17
@@ -42,13 +42,13 @@ module Gollum
|
|||||||
if sha?(ref)
|
if sha?(ref)
|
||||||
ref
|
ref
|
||||||
else
|
else
|
||||||
@ref_map[ref] ||= ref_to_sha!(ref)
|
get_cache(:ref, ref) { ref_to_sha!(ref) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def tree(ref)
|
def tree(ref)
|
||||||
sha = ref_to_sha(ref)
|
sha = ref_to_sha(ref)
|
||||||
@tree_map[sha] ||= tree!(sha)
|
get_cache(:tree, sha) { tree!(sha) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def blob(sha)
|
def blob(sha)
|
||||||
@@ -57,21 +57,21 @@ module Gollum
|
|||||||
|
|
||||||
def commit(ref)
|
def commit(ref)
|
||||||
if sha?(ref)
|
if sha?(ref)
|
||||||
@commit_map[ref] ||= commit!(ref)
|
get_cache(:commit, ref) { commit!(ref) }
|
||||||
else
|
else
|
||||||
if sha = @ref_map[ref]
|
if sha = get_cache(:ref, ref)
|
||||||
commit(sha)
|
commit(sha)
|
||||||
else
|
else
|
||||||
cm = commit!(ref)
|
cm = commit!(ref)
|
||||||
@ref_map[ref] = cm.id
|
set_cache(:ref, ref, cm.id)
|
||||||
@commit_map[cm.id] = cm
|
set_cache(:commit, cm.id, cm)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def commits(*shas)
|
def commits(*shas)
|
||||||
shas.flatten!
|
shas.flatten!
|
||||||
cached_commits = multi_get(:commit_map, shas)
|
cached_commits = multi_get(:commit, shas)
|
||||||
missing_shas = shas.select do |sha|
|
missing_shas = shas.select do |sha|
|
||||||
!cached_commits.key?(sha)
|
!cached_commits.key?(sha)
|
||||||
end
|
end
|
||||||
@@ -85,16 +85,6 @@ module Gollum
|
|||||||
shas.map { |sha| cached_commits[sha] }
|
shas.map { |sha| cached_commits[sha] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def multi_get(name, keys)
|
|
||||||
value = instance_variable_get("@#{name}")
|
|
||||||
keys.inject({}) do |memo, key|
|
|
||||||
if v = value[key]
|
|
||||||
memo[key] = v
|
|
||||||
end
|
|
||||||
memo
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def sha?(str)
|
def sha?(str)
|
||||||
str =~ /^[0-9a-f]{40}$/
|
str =~ /^[0-9a-f]{40}$/
|
||||||
end
|
end
|
||||||
@@ -119,6 +109,30 @@ module Gollum
|
|||||||
@repo.commit(sha)
|
@repo.commit(sha)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_cache(name, key)
|
||||||
|
cache = instance_variable_get("@#{name}_map")
|
||||||
|
value = cache[key]
|
||||||
|
if value.nil? && block_given?
|
||||||
|
set_cache(name, key, value = yield)
|
||||||
|
end
|
||||||
|
value
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_cache(name, key, value)
|
||||||
|
cache = instance_variable_get("@#{name}_map")
|
||||||
|
cache[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def multi_get(name, keys)
|
||||||
|
value = instance_variable_get("@#{name}_map")
|
||||||
|
keys.inject({}) do |memo, key|
|
||||||
|
if v = value[key]
|
||||||
|
memo[key] = v
|
||||||
|
end
|
||||||
|
memo
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Parses a line of output from the `ls-tree` command.
|
# Parses a line of output from the `ls-tree` command.
|
||||||
#
|
#
|
||||||
# line - A String line of output:
|
# line - A String line of output:
|
||||||
|
|||||||
Reference in New Issue
Block a user