Commit 06f0c8c3 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'cached-mr-api' into 'master'

Cache entities for MR API [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!61067
parents 2b6f1126 bd013deb
---
name: api_caching_merge_requests
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/61067
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/330342
milestone: '13.12'
type: development
group: group::source code
default_enabled: false
......@@ -40,7 +40,7 @@ module API
# @param expires_in [ActiveSupport::Duration, Integer] an expiry time for the cache entry
# @param presenter_args [Hash] keyword arguments to be passed to the entity
# @return [Gitlab::Json::PrecompiledJson]
def present_cached(obj_or_collection, with:, cache_context: -> (_) { current_user.cache_key }, expires_in: DEFAULT_EXPIRY, **presenter_args)
def present_cached(obj_or_collection, with:, cache_context: -> (_) { current_user&.cache_key }, expires_in: DEFAULT_EXPIRY, **presenter_args)
json =
if obj_or_collection.is_a?(Enumerable)
cached_collection(
......
......@@ -201,7 +201,11 @@ module API
options = serializer_options_for(merge_requests).merge(project: user_project)
options[:project] = user_project
present merge_requests, options
if Feature.enabled?(:api_caching_merge_requests, user_project, type: :development, default_enabled: :yaml)
present_cached merge_requests, expires_in: 10.minutes, **options
else
present merge_requests, options
end
end
desc 'Create a merge request' do
......
......@@ -52,7 +52,7 @@ RSpec.describe API::MergeRequests do
end
context 'when authenticated' do
it 'avoids N+1 queries' do
it 'avoids N+1 queries', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/330335' do
control = ActiveRecord::QueryRecorder.new do
get api(endpoint_path, user)
end
......@@ -142,7 +142,7 @@ RSpec.describe API::MergeRequests do
expect(json_response.last['labels'].first).to match_schema('/public_api/v4/label_basic')
end
it 'avoids N+1 queries' do
it 'avoids N+1 queries', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/330335' do
path = endpoint_path + "?with_labels_details=true"
control = ActiveRecord::QueryRecorder.new do
......@@ -973,6 +973,14 @@ RSpec.describe API::MergeRequests do
it_behaves_like 'merge requests list'
context 'when :api_caching_merge_requests is disabled' do
before do
stub_feature_flags(api_caching_merge_requests: false)
end
it_behaves_like 'merge requests list'
end
it "returns 404 for non public projects" do
project = create(:project, :private)
......@@ -1049,7 +1057,7 @@ RSpec.describe API::MergeRequests do
include_context 'with merge requests'
it 'avoids N+1 queries' do
it 'avoids N+1 queries', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/330335' do
control = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/merge_requests", user)
end.count
......
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