diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-02-08 14:50:55 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2016-02-08 17:40:19 +0300 |
commit | 9a99d8e49dc07faaaa2fae436423e11dab5a7d7e (patch) | |
tree | de208a478614dfc317f444969480fa56013ee809 /app/models/repository.rb | |
parent | 7322c5a05bc018108123194e4e542bb04d42b2b2 (diff) |
Cache various Repository Git operations
This caches the output of the following methods:
* Repository#empty?
* Repository#has_visible_content?
* Repository#root_ref
The cache for Repository#has_visible_content? is flushed whenever a
commit is pushed to a new branch or an existing branch is removed.
The cache for Repository#root_ref is only flushed whenever a user
changes the default branch of a project. The cache for Repository#empty?
is never explicitly flushed as there's no need for it.
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r-- | app/models/repository.rb | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index e813c946bc1..27bdbac3e52 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -44,7 +44,9 @@ class Repository end def empty? - raw_repository.empty? + return @empty unless @empty.nil? + + @empty = cache.fetch(:empty?) { raw_repository.empty? } end # @@ -57,7 +59,11 @@ class Repository # This method return true if repository contains some content visible in project page. # def has_visible_content? - raw_repository.branch_count > 0 + return @has_visible_content unless @has_visible_content.nil? + + @has_visible_content = cache.fetch(:has_visible_content?) do + raw_repository.branch_count > 0 + end end def commit(id = 'HEAD') @@ -243,6 +249,16 @@ class Repository end end + def expire_root_ref_cache + cache.expire(:root_ref) + @root_ref = nil + end + + def expire_has_visible_content_cache + cache.expire(:has_visible_content?) + @has_visible_content = nil + end + def rebuild_cache cache_keys.each do |key| cache.expire(key) @@ -480,7 +496,7 @@ class Repository end def root_ref - @root_ref ||= raw_repository.root_ref + @root_ref ||= cache.fetch(:root_ref) { raw_repository.root_ref } end def commit_dir(user, path, message, branch) |