Commit a5db2157 authored by Winnie Hellmann's avatar Winnie Hellmann Committed by Stan Hu

Merge branch 'fix-application-setting-nil-cache' into 'master'

Prevent ApplicationSetting to cache nil value

Closes #39275

See merge request gitlab-org/gitlab-ce!14952
parent cb3faabc
...@@ -228,7 +228,10 @@ class ApplicationSetting < ActiveRecord::Base ...@@ -228,7 +228,10 @@ class ApplicationSetting < ActiveRecord::Base
ensure_cache_setup ensure_cache_setup
Rails.cache.fetch(CACHE_KEY) do Rails.cache.fetch(CACHE_KEY) do
ApplicationSetting.last ApplicationSetting.last.tap do |settings|
# do not cache nils
raise 'missing settings' unless settings
end
end end
rescue rescue
# Fall back to an uncached value if there are any problems (e.g. redis down) # Fall back to an uncached value if there are any problems (e.g. redis down)
......
---
title: Fix application setting to cache nil object
merge_request:
author:
type: fixed
...@@ -29,7 +29,7 @@ describe Gitlab::CurrentSettings do ...@@ -29,7 +29,7 @@ describe Gitlab::CurrentSettings do
it 'falls back to DB if Caching returns an empty value' do it 'falls back to DB if Caching returns an empty value' do
expect(ApplicationSetting).to receive(:cached).and_return(nil) expect(ApplicationSetting).to receive(:cached).and_return(nil)
expect(ApplicationSetting).to receive(:last).and_call_original expect(ApplicationSetting).to receive(:last).and_call_original.twice
expect(current_application_settings).to be_a(ApplicationSetting) expect(current_application_settings).to be_a(ApplicationSetting)
end end
......
...@@ -231,6 +231,21 @@ describe ApplicationSetting do ...@@ -231,6 +231,21 @@ describe ApplicationSetting do
expect(described_class.current).to eq(:last) expect(described_class.current).to eq(:last)
end end
end end
context 'when an ApplicationSetting is not yet present' do
it 'does not cache nil object' do
# when missing settings a nil object is returned, but not cached
allow(described_class).to receive(:last).and_return(nil).twice
expect(described_class.current).to be_nil
# when the settings are set the method returns a valid object
allow(described_class).to receive(:last).and_return(:last)
expect(described_class.current).to eq(:last)
# subsequent calls get everything from cache
expect(described_class.current).to eq(:last)
end
end
end end
context 'restrict creating duplicates' do context 'restrict creating duplicates' 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