Commit 5b677ab4 authored by Patrick Bajao's avatar Patrick Bajao

Preload commits markdown cache

This is for improving the performance of
Projects::MergeRequestsController#commits action.

Prevents n+1 requests to Redis when getting the value from cache
on render if the cache is warm.

Previously, it'll make an HMGET request to Redis per commit which
can result to 100 requests when viewing the commits in a MR.

Now, a single Redis pipeline is used via `with_markdown_cache`.
parent fc0d2286
......@@ -109,8 +109,8 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
# or from cache if already merged
@commits =
set_commits_for_rendering(
@merge_request.recent_commits.with_latest_pipeline(@merge_request.source_branch),
commits_count: @merge_request.commits_count
@merge_request.recent_commits.with_latest_pipeline(@merge_request.source_branch).with_markdown_cache,
commits_count: @merge_request.commits_count
)
render json: { html: view_to_html_string('projects/merge_requests/_commits') }
......
......@@ -53,6 +53,17 @@ class CommitCollection
self
end
# Returns the collection with markdown fields preloaded.
#
# Get the markdown cache from redis using pipeline to prevent n+1 requests
# when rendering the markdown of an attribute (e.g. title, full_title,
# description).
def with_markdown_cache
Commit.preload_markdown_cache!(commits)
self
end
def unenriched
commits.reject(&:gitaly_commit?)
end
......
---
title: Preload commits markdown cache
merge_request: 35314
author:
type: performance
......@@ -75,6 +75,18 @@ RSpec.describe CommitCollection do
end
end
describe '#with_markdown_cache' do
let(:commits) { [commit] }
let(:collection) { described_class.new(project, commits) }
it 'preloads commits cache markdown' do
aggregate_failures do
expect(Commit).to receive(:preload_markdown_cache!).with(commits)
expect(collection.with_markdown_cache).to eq(collection)
end
end
end
describe 'enrichment methods' do
let(:gitaly_commit) { commit }
let(:hash_commit) { Commit.from_hash(gitaly_commit.to_hash, project) }
......
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