Use serialize_keys as part of highlight diff cache

This assures that old cache is not used if we change format of
cached data.
This commit is contained in:
Jan Provaznik 2018-07-04 11:34:35 +02:00
parent 4c1a2a9b99
commit dc551581d0
4 changed files with 18 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
title: Invalidate merge request diffs cache if diff data change.
merge_request:
author:
type: fixed

View File

@ -34,7 +34,7 @@ module Gitlab
end
def cache_key
[@merge_request_diff, 'highlighted-diff-files', diff_options]
[@merge_request_diff, 'highlighted-diff-files', Gitlab::Diff::Line::SERIALIZE_KEYS, diff_options]
end
private

View File

@ -1,6 +1,8 @@
module Gitlab
module Diff
class Line
SERIALIZE_KEYS = %i(line_code text type index old_pos new_pos).freeze
attr_reader :line_code, :type, :index, :old_pos, :new_pos
attr_writer :rich_text
attr_accessor :text
@ -19,13 +21,9 @@ module Gitlab
new(hash[:text], hash[:type], hash[:index], hash[:old_pos], hash[:new_pos], line_code: hash[:line_code])
end
def serialize_keys
@serialize_keys ||= %i(line_code text type index old_pos new_pos)
end
def to_hash
hash = {}
serialize_keys.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend
SERIALIZE_KEYS.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend
hash
end

View File

@ -20,6 +20,15 @@ describe Gitlab::Diff::FileCollection::MergeRequestDiff do
diff_files
end
it 'it uses a different cache key if diff line keys change' do
mr_diff = described_class.new(merge_request.merge_request_diff, diff_options: nil)
key = mr_diff.cache_key
stub_const('Gitlab::Diff::Line::SERIALIZE_KEYS', [:foo])
expect(mr_diff.cache_key).not_to eq(key)
end
shared_examples 'initializes a DiffCollection' do
it 'returns a valid instance of a DiffCollection' do
expect(diff_files).to be_a(Gitlab::Git::DiffCollection)