Commit b49afc54 authored by Robert May's avatar Robert May

Cache rendered compare entity

Adds caching for the rendered compare JSON, and adds a new
cache key method to ensure it expires correctly.

Changelog: performance
parent 8ddd7787
......@@ -25,6 +25,10 @@ class Compare
@straight = straight
end
def cache_key
[@project, :compare, start_commit_sha, base_commit_sha, head_commit_sha, @straight]
end
def commits
@commits ||= Commit.decorate(@compare.commits, project)
end
......
---
name: api_caching_repository_compare
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64418
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/334264
milestone: '14.1'
type: development
group: group::source code
default_enabled: false
......@@ -138,7 +138,11 @@ module API
compare = CompareService.new(user_project, params[:to]).execute(target_project, params[:from], straight: params[:straight])
if compare
present compare, with: Entities::Compare
if Feature.enabled?(:api_caching_repository_compare, user_project, default_enabled: :yaml)
present_cached compare, with: Entities::Compare, expires_in: 1.day, cache_context: nil
else
present compare, with: Entities::Compare
end
else
not_found!("Ref")
end
......
......@@ -13,7 +13,16 @@ RSpec.describe Compare do
let(:raw_compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, start_commit.id, head_commit.id) }
subject { described_class.new(raw_compare, project) }
subject(:compare) { described_class.new(raw_compare, project) }
describe '#cache_key' do
subject { compare.cache_key }
it { is_expected.to include(project) }
it { is_expected.to include(start_commit.id) }
it { is_expected.to include(head_commit.id) }
it { is_expected.to include(compare.base_commit_sha) }
end
describe '#start_commit' do
it 'returns raw compare base commit' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment