summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2017-10-19 15:07:18 +0300
committerWinnie Hellmann <winnie@gitlab.com>2017-10-21 23:32:05 +0300
commit8783c1345461ae2de9957eb1c5515ea93206ea2f (patch)
treeb221ae37eed9b7445e5b5bf8ac4e4ca3aa5b6684
parent1bba97004eae8256403eb34f29fc45500b8e2809 (diff)
downloadgitlab-ce-8783c1345461ae2de9957eb1c5515ea93206ea2f.tar.gz
Prevent ApplicationSetting to cache nil value
-rw-r--r--app/models/application_setting.rb5
-rw-r--r--changelogs/unreleased/fix-application-setting-nil-cache.yml5
-rw-r--r--spec/models/application_setting_spec.rb15
3 files changed, 24 insertions, 1 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
index d3b8debb0fd..4dda276bb41 100644
--- a/app/models/application_setting.rb
+++ b/app/models/application_setting.rb
@@ -203,7 +203,10 @@ class ApplicationSetting < ActiveRecord::Base
ensure_cache_setup
Rails.cache.fetch(CACHE_KEY) do
- ApplicationSetting.last
+ ApplicationSetting.last.tap do |settings|
+ # do not cache nils
+ raise 'missing settings' unless settings
+ end
end
rescue
# Fall back to an uncached value if there are any problems (e.g. redis down)
diff --git a/changelogs/unreleased/fix-application-setting-nil-cache.yml b/changelogs/unreleased/fix-application-setting-nil-cache.yml
new file mode 100644
index 00000000000..a5f028e3d69
--- /dev/null
+++ b/changelogs/unreleased/fix-application-setting-nil-cache.yml
@@ -0,0 +1,5 @@
+---
+title: Fix application setting to cache nil object
+merge_request:
+author:
+type: fixed
diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb
index 6945c90cb9b..30495fd4f5e 100644
--- a/spec/models/application_setting_spec.rb
+++ b/spec/models/application_setting_spec.rb
@@ -220,6 +220,21 @@ describe ApplicationSetting do
expect(described_class.current).to eq(:last)
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
context 'restrict creating duplicates' do