Commit d3666c79 authored by David Fernandez's avatar David Fernandez

Merge branch '331297_use_caching_version_attribute' into 'master'

Use `version` from ActiveSupport::Cache::Store

See merge request gitlab-org/gitlab!65436
parents 95fbdf4d ecbaa530
...@@ -11,12 +11,10 @@ module API ...@@ -11,12 +11,10 @@ module API
include Gitlab::Cache::Helpers include Gitlab::Cache::Helpers
# @return [Hash] # @return [Hash]
DEFAULT_CACHE_OPTIONS = { DEFAULT_CACHE_OPTIONS = {
race_condition_ttl: 5.seconds race_condition_ttl: 5.seconds,
version: 1
}.freeze }.freeze
# @return Integer
VERSION = 1
# @return [Array] # @return [Array]
PAGINATION_HEADERS = %w[X-Per-Page X-Page X-Next-Page X-Prev-Page Link X-Total X-Total-Pages].freeze PAGINATION_HEADERS = %w[X-Per-Page X-Page X-Next-Page X-Prev-Page Link X-Total X-Total-Pages].freeze
...@@ -81,7 +79,7 @@ module API ...@@ -81,7 +79,7 @@ module API
def cache_action(key, **custom_cache_opts) def cache_action(key, **custom_cache_opts)
cache_opts = apply_default_cache_options(custom_cache_opts) cache_opts = apply_default_cache_options(custom_cache_opts)
json, cached_headers = cache.fetch([key, VERSION], **cache_opts) do json, cached_headers = cache.fetch(key, **cache_opts) do
response = yield response = yield
cached_body = response.is_a?(Gitlab::Json::PrecompiledJson) ? response.to_s : Gitlab::Json.dump(response.as_json) cached_body = response.is_a?(Gitlab::Json::PrecompiledJson) ? response.to_s : Gitlab::Json.dump(response.as_json)
......
...@@ -81,7 +81,7 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do ...@@ -81,7 +81,7 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do
expected_kwargs = described_class::DEFAULT_CACHE_OPTIONS.merge(kwargs) expected_kwargs = described_class::DEFAULT_CACHE_OPTIONS.merge(kwargs)
expect(expensive_thing).to receive(:do_very_expensive_action).once expect(expensive_thing).to receive(:do_very_expensive_action).once
expect(instance.cache).to receive(:fetch).with([cache_key, 1], **expected_kwargs).exactly(5).times.and_call_original expect(instance.cache).to receive(:fetch).with(cache_key, **expected_kwargs).exactly(5).times.and_call_original
5.times { perform } 5.times { perform }
end end
...@@ -96,6 +96,16 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do ...@@ -96,6 +96,16 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do
expect(nested_call.to_s).to eq(subject.to_s) expect(nested_call.to_s).to eq(subject.to_s)
end end
context 'Cache versioning' do
it 'returns cache based on version parameter' do
result_1 = instance.cache_action(cache_key, **kwargs.merge(version: 1)) { 'Cache 1' }
result_2 = instance.cache_action(cache_key, **kwargs.merge(version: 2)) { 'Cache 2' }
expect(result_1.to_s).to eq('Cache 1'.to_json)
expect(result_2.to_s).to eq('Cache 2'.to_json)
end
end
context 'Cache for pagination headers' do context 'Cache for pagination headers' do
described_class::PAGINATION_HEADERS.each do |pagination_header| described_class::PAGINATION_HEADERS.each do |pagination_header|
context pagination_header do context pagination_header do
......
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