From 0e7a9e46d25813670bc9d37b59d577bda4aeb3f7 Mon Sep 17 00:00:00 2001 From: Reuben Pereira Date: Mon, 18 Feb 2019 11:24:00 +0000 Subject: Allow blank values to be stored in reactive cache Reactive caching concern was using .present? to determine if it got a valid value from the cache. This returns false for values such as false, [], {}. Change this check to !.nil? instead. --- app/models/concerns/reactive_caching.rb | 2 +- .../unreleased/56851-blank-values-in-reactive-cache.yml | 5 +++++ spec/models/concerns/reactive_caching_spec.rb | 12 ++++++++++-- spec/support/helpers/reactive_caching_helpers.rb | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/56851-blank-values-in-reactive-cache.yml diff --git a/app/models/concerns/reactive_caching.rb b/app/models/concerns/reactive_caching.rb index d3572875fb3..de77ca3e963 100644 --- a/app/models/concerns/reactive_caching.rb +++ b/app/models/concerns/reactive_caching.rb @@ -76,7 +76,7 @@ module ReactiveCaching begin data = Rails.cache.read(full_reactive_cache_key(*args)) - yield data if data.present? + yield data unless data.nil? rescue InvalidateReactiveCache refresh_reactive_cache!(*args) nil diff --git a/changelogs/unreleased/56851-blank-values-in-reactive-cache.yml b/changelogs/unreleased/56851-blank-values-in-reactive-cache.yml new file mode 100644 index 00000000000..5b9253793be --- /dev/null +++ b/changelogs/unreleased/56851-blank-values-in-reactive-cache.yml @@ -0,0 +1,5 @@ +--- +title: Allow empty values such as [] to be stored in reactive cache +merge_request: 25283 +author: +type: fixed diff --git a/spec/models/concerns/reactive_caching_spec.rb b/spec/models/concerns/reactive_caching_spec.rb index 97a4c212f1c..03ae45e6b17 100644 --- a/spec/models/concerns/reactive_caching_spec.rb +++ b/spec/models/concerns/reactive_caching_spec.rb @@ -25,7 +25,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do def result with_reactive_cache do |data| - data / 2 + data end end end @@ -64,7 +64,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do stub_reactive_cache(instance, 4) end - it { is_expected.to eq(2) } + it { is_expected.to eq(4) } it 'does not enqueue a background worker' do expect(ReactiveCachingWorker).not_to receive(:perform_async) @@ -94,6 +94,14 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do end end end + + context 'when cache contains non-nil but blank value' do + before do + stub_reactive_cache(instance, false) + end + + it { is_expected.to eq(false) } + end end describe '#clear_reactive_cache!' do diff --git a/spec/support/helpers/reactive_caching_helpers.rb b/spec/support/helpers/reactive_caching_helpers.rb index a575aa99b79..b76b53db0b9 100644 --- a/spec/support/helpers/reactive_caching_helpers.rb +++ b/spec/support/helpers/reactive_caching_helpers.rb @@ -10,7 +10,7 @@ module ReactiveCachingHelpers def stub_reactive_cache(subject = nil, data = nil, *qualifiers) allow(ReactiveCachingWorker).to receive(:perform_async) allow(ReactiveCachingWorker).to receive(:perform_in) - write_reactive_cache(subject, data, *qualifiers) if data + write_reactive_cache(subject, data, *qualifiers) unless data.nil? end def synchronous_reactive_cache(subject) -- cgit v1.2.1