Commit 0cb0aaee authored by Patrick Bajao's avatar Patrick Bajao

Add cache version and cached markdown version on cache key

This is needed so we can invalidate the cache by bumping the
CACHE_VERSION and also gets invalidated when the cached markdown
version gets bump.
parent 5eea8a67
...@@ -7,6 +7,9 @@ class Discussion ...@@ -7,6 +7,9 @@ class Discussion
include GlobalID::Identification include GlobalID::Identification
include ResolvableDiscussion include ResolvableDiscussion
# Bump this if we need to refresh the cached versions of discussions
CACHE_VERSION = 1
attr_reader :notes, :context_noteable attr_reader :notes, :context_noteable
delegate :created_at, delegate :created_at,
...@@ -165,6 +168,8 @@ class Discussion ...@@ -165,6 +168,8 @@ class Discussion
notes_sha = Digest::SHA1.hexdigest(notes.map(&:id).join(':')) notes_sha = Digest::SHA1.hexdigest(notes.map(&:id).join(':'))
[ [
CACHE_VERSION,
notes.last.latest_cached_markdown_version,
id, id,
notes_sha, notes_sha,
notes.max_by(&:updated_at).updated_at, notes.max_by(&:updated_at).updated_at,
......
...@@ -56,7 +56,7 @@ RSpec.describe Discussion do ...@@ -56,7 +56,7 @@ RSpec.describe Discussion do
let(:notes_sha) { Digest::SHA1.hexdigest("#{first_note.id}:#{second_note.id}:#{third_note.id}") } let(:notes_sha) { Digest::SHA1.hexdigest("#{first_note.id}:#{second_note.id}:#{third_note.id}") }
it 'returns the cache key with ID and latest updated note updated at' do it 'returns the cache key with ID and latest updated note updated at' do
expect(subject.cache_key).to eq("#{subject.id}:#{notes_sha}:#{third_note.updated_at}:") expect(subject.cache_key).to eq("#{described_class::CACHE_VERSION}:#{third_note.latest_cached_markdown_version}:#{subject.id}:#{notes_sha}:#{third_note.updated_at}:")
end end
context 'when discussion is resolved' do context 'when discussion is resolved' do
...@@ -65,7 +65,7 @@ RSpec.describe Discussion do ...@@ -65,7 +65,7 @@ RSpec.describe Discussion do
end end
it 'returns the cache key with resolved at' do it 'returns the cache key with resolved at' do
expect(subject.cache_key).to eq("#{subject.id}:#{notes_sha}:#{third_note.updated_at}:#{subject.resolved_at}") expect(subject.cache_key).to eq("#{described_class::CACHE_VERSION}:#{third_note.latest_cached_markdown_version}:#{subject.id}:#{notes_sha}:#{third_note.updated_at}:#{subject.resolved_at}")
end end
end end
end end
......
...@@ -147,6 +147,17 @@ RSpec.describe 'merge requests discussions' do ...@@ -147,6 +147,17 @@ RSpec.describe 'merge requests discussions' do
end end
end end
context 'when cached markdown version gets bump' do
before do
settings = Gitlab::CurrentSettings.current_application_settings
settings.update!(local_markdown_version: settings.local_markdown_version + 1)
end
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
end
end
context 'when merge_request_discussion_cache is disabled' do context 'when merge_request_discussion_cache is disabled' do
before do before do
stub_feature_flags(merge_request_discussion_cache: false) stub_feature_flags(merge_request_discussion_cache: false)
......
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