From 9a99d8e49dc07faaaa2fae436423e11dab5a7d7e Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 8 Feb 2016 12:50:55 +0100 Subject: 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. --- app/models/repository.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'app/models/repository.rb') 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) -- cgit v1.2.1