Commit 65998716 authored by Brian Kabiro's avatar Brian Kabiro Committed by Nick Thomas

Check if mapping is empty before caching

- check if mapping is empty before calling `write_multiple`
- add spec
parent 1044e5d4
---
title: Check if mapping is empty before caching in File Collections
merge_request: 18290
author: briankabiro
type: performance
......@@ -27,12 +27,14 @@ module Gitlab
# - The cache content is not updated (there's no need to do so)
def load_highlight
ids = highlightable_collection_ids
return if ids.empty?
cached_content = read_cache(ids)
uncached_ids = ids.select.each_with_index { |_, i| cached_content[i].nil? }
mapping = highlighted_lines_by_ids(uncached_ids)
HighlightCache.write_multiple(mapping)
HighlightCache.write_multiple(mapping) if mapping.any?
diffs = diff_files_indexed_by_id.values_at(*ids)
......
......@@ -40,6 +40,14 @@ describe Gitlab::DiscussionsDiff::FileCollection do
subject.load_highlight
end
it 'does not write cache for empty mapping' do
allow(subject).to receive(:highlighted_lines_by_ids).and_return([])
expect(Gitlab::DiscussionsDiff::HighlightCache).not_to receive(:write_multiple)
subject.load_highlight
end
it 'does not write cache for resolved notes' do
diff_note_a.update_column(:resolved_at, Time.now)
......
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