Commit 0e7a9e46 authored by Reuben Pereira's avatar Reuben Pereira Committed by Nick Thomas

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.
parent 1d229689
...@@ -76,7 +76,7 @@ module ReactiveCaching ...@@ -76,7 +76,7 @@ module ReactiveCaching
begin begin
data = Rails.cache.read(full_reactive_cache_key(*args)) data = Rails.cache.read(full_reactive_cache_key(*args))
yield data if data.present? yield data unless data.nil?
rescue InvalidateReactiveCache rescue InvalidateReactiveCache
refresh_reactive_cache!(*args) refresh_reactive_cache!(*args)
nil nil
......
---
title: Allow empty values such as [] to be stored in reactive cache
merge_request: 25283
author:
type: fixed
...@@ -25,7 +25,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -25,7 +25,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
def result def result
with_reactive_cache do |data| with_reactive_cache do |data|
data / 2 data
end end
end end
end end
...@@ -64,7 +64,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -64,7 +64,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
stub_reactive_cache(instance, 4) stub_reactive_cache(instance, 4)
end end
it { is_expected.to eq(2) } it { is_expected.to eq(4) }
it 'does not enqueue a background worker' do it 'does not enqueue a background worker' do
expect(ReactiveCachingWorker).not_to receive(:perform_async) expect(ReactiveCachingWorker).not_to receive(:perform_async)
...@@ -94,6 +94,14 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -94,6 +94,14 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end end
end 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 end
describe '#clear_reactive_cache!' do describe '#clear_reactive_cache!' do
......
...@@ -10,7 +10,7 @@ module ReactiveCachingHelpers ...@@ -10,7 +10,7 @@ module ReactiveCachingHelpers
def stub_reactive_cache(subject = nil, data = nil, *qualifiers) def stub_reactive_cache(subject = nil, data = nil, *qualifiers)
allow(ReactiveCachingWorker).to receive(:perform_async) allow(ReactiveCachingWorker).to receive(:perform_async)
allow(ReactiveCachingWorker).to receive(:perform_in) allow(ReactiveCachingWorker).to receive(:perform_in)
write_reactive_cache(subject, data, *qualifiers) if data write_reactive_cache(subject, data, *qualifiers) unless data.nil?
end end
def synchronous_reactive_cache(subject) def synchronous_reactive_cache(subject)
......
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