Commit 393556a1 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'sh-better-diff-stats-versioning' into 'master'

Use Gitaly protobuf version as DiffStats cache key

Closes #233083

See merge request gitlab-org/gitlab!38414
parents d6146737 56cee5a2
---
title: Use Gitaly protobuf version as DiffStats cache key
merge_request: 38414
author:
type: fixed
......@@ -6,7 +6,8 @@ module Gitlab
include Gitlab::Utils::StrongMemoize
EXPIRATION = 1.week
VERSION = 1
# The DiffStats#as_json representation is tied to the Gitaly protobuf version
VERSION = Gem.loaded_specs['gitaly'].version.to_s
def initialize(cachable_key:)
@cachable_key = cachable_key
......@@ -29,6 +30,7 @@ module Gitlab
return unless stats
cache.write(key, stats.as_json, expires_in: EXPIRATION)
clear_memoization(:cached_values)
end
def clear
......
......@@ -81,4 +81,28 @@ RSpec.describe Gitlab::Diff::StatsCache, :use_clean_rails_memory_store_caching d
stats_cache.clear
end
end
it 'VERSION is set' do
expect(described_class::VERSION).to be_present
end
context 'with multiple cache versions' do
before do
stats_cache.write_if_empty(stats)
end
it 'does not read from a stale cache' do
expect(stats_cache.read.to_json).to eq(stats.to_json)
stub_const('Gitlab::Diff::StatsCache::VERSION', '1.0.new-new-thing')
stats_cache = described_class.new(cachable_key: cachable_key)
expect(stats_cache.read).to be_nil
stats_cache.write_if_empty(stats)
expect(stats_cache.read.to_json).to eq(stats.to_json)
end
end
end
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