From c85f9c5b1d320f7a6f75e3d08bbafd2fb20d3f58 Mon Sep 17 00:00:00 2001 From: Francisco Lopez Date: Fri, 24 Nov 2017 17:37:02 +0100 Subject: Code review comments applied --- app/services/base_count_service.rb | 4 ++++ app/services/projects/batch_count_service.rb | 8 ++++++-- lib/api/groups.rb | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/services/base_count_service.rb b/app/services/base_count_service.rb index 2e4fd1db03e..f2844854112 100644 --- a/app/services/base_count_service.rb +++ b/app/services/base_count_service.rb @@ -12,6 +12,10 @@ class BaseCountService Rails.cache.fetch(cache_key, cache_options) { uncached_count }.to_i end + def count_stored? + Rails.cache.read(cache_key).present? + end + def refresh_cache(&block) Rails.cache.write(cache_key, block_given? ? yield : uncached_count, raw: raw?) end diff --git a/app/services/projects/batch_count_service.rb b/app/services/projects/batch_count_service.rb index 5b69e24b893..b8a030e7bc5 100644 --- a/app/services/projects/batch_count_service.rb +++ b/app/services/projects/batch_count_service.rb @@ -12,7 +12,9 @@ module Projects def refresh_cache @projects.each do |project| - current_count_service(project).refresh_cache { global_count[project.id].to_i } + unless current_count_service(project).count_stored? + current_count_service(project).refresh_cache { global_count[project.id].to_i } + end end end @@ -20,8 +22,10 @@ module Projects if defined? @service @service.project = project else - count_service.new(project) + @service = count_service.new(project) end + + @service end def global_count(project) diff --git a/lib/api/groups.rb b/lib/api/groups.rb index 05443329a32..83c7898150a 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -64,8 +64,8 @@ module API forked_from_project: [:route, :forks, namespace: :route, tags: :taggings]) projects = reorder_projects(projects) projects = paginate(projects) - projects_with_fork = projects + projects.map(&:forked_from_project).compact - ::Projects::BatchForksCountService.new(projects_with_fork).refresh_cache + projects_with_forks = projects + projects.map(&:forked_from_project).compact + ::Projects::BatchForksCountService.new(projects_with_forks).refresh_cache ::Projects::BatchOpenIssuesCountService.new(projects).refresh_cache projects end -- cgit v1.2.1