summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2018-11-26 12:14:35 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2018-11-26 12:14:35 +0000
commitf9c6134b60465a5628c36d396e7f81f04c3ea235 (patch)
treefb8d22d72c843ef5e063a1aafba1fca71c0c9abb /lib
parentbf99a58852b10a5593dbb7f9d307bafabb5d3d5f (diff)
parent0c3005242734708e5282b2c8cd631e0a90ec15d1 (diff)
downloadgitlab-ce-f9c6134b60465a5628c36d396e7f81f04c3ea235.tar.gz
Merge branch '40260-reduce-gitaly-calls-project-pipeline-status' into 'master'
Cache project HEAD to prevent unnecessary Gitaly calls See merge request gitlab-org/gitlab-ce!23307
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/cache/ci/project_pipeline_status.rb40
1 files changed, 12 insertions, 28 deletions
diff --git a/lib/gitlab/cache/ci/project_pipeline_status.rb b/lib/gitlab/cache/ci/project_pipeline_status.rb
index 78b0eaac8cd..ea7013db2ce 100644
--- a/lib/gitlab/cache/ci/project_pipeline_status.rb
+++ b/lib/gitlab/cache/ci/project_pipeline_status.rb
@@ -7,9 +7,9 @@ module Gitlab
module Cache
module Ci
class ProjectPipelineStatus
- attr_accessor :sha, :status, :ref, :project, :loaded
+ include Gitlab::Utils::StrongMemoize
- delegate :commit, to: :project
+ attr_accessor :sha, :status, :ref, :project, :loaded
def self.load_for_project(project)
new(project).tap do |status|
@@ -18,33 +18,12 @@ module Gitlab
end
def self.load_in_batch_for_projects(projects)
- cached_results_for_projects(projects).zip(projects).each do |result, project|
- project.pipeline_status = new(project, result)
+ projects.each do |project|
+ project.pipeline_status = new(project)
project.pipeline_status.load_status
end
end
- def self.cached_results_for_projects(projects)
- result = Gitlab::Redis::Cache.with do |redis|
- redis.multi do
- projects.each do |project|
- cache_key = cache_key_for_project(project)
- redis.exists(cache_key)
- redis.hmget(cache_key, :sha, :status, :ref)
- end
- end
- end
-
- result.each_slice(2).map do |(cache_key_exists, (sha, status, ref))|
- pipeline_info = { sha: sha, status: status, ref: ref }
- { loaded_from_cache: cache_key_exists, pipeline_info: pipeline_info }
- end
- end
-
- def self.cache_key_for_project(project)
- "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:project:#{project.id}:pipeline_status:#{project.commit&.sha}"
- end
-
def self.update_for_pipeline(pipeline)
pipeline_info = {
sha: pipeline.sha,
@@ -70,6 +49,7 @@ module Gitlab
def load_status
return if loaded?
+ return unless commit
if has_cache?
load_from_cache
@@ -82,8 +62,6 @@ module Gitlab
end
def load_from_project
- return unless commit
-
self.sha, self.status, self.ref = commit.sha, commit.status, project.default_branch
end
@@ -132,7 +110,13 @@ module Gitlab
end
def cache_key
- self.class.cache_key_for_project(project)
+ "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:project:#{project.id}:pipeline_status:#{commit&.sha}"
+ end
+
+ def commit
+ strong_memoize(:commit) do
+ project.commit
+ end
end
end
end