Commit 67de299b authored by Fabio Pitino's avatar Fabio Pitino

Allow ReactiveCaching to support nil value

When :calculate_reactive_caching returns a nil value
this caused ReactiveCaching to schedule a worker
every time the code using :with_reactive_cache was called.

This issue caused an increasing amount of Sidekiq jobs
being created continuously.

Implementing this fix behind feature flag
:reactive_caching_check_key_exists
parent ededb334
...@@ -173,7 +173,11 @@ module ReactiveCaching ...@@ -173,7 +173,11 @@ module ReactiveCaching
end end
def within_reactive_cache_lifetime?(*args) def within_reactive_cache_lifetime?(*args)
!!Rails.cache.read(alive_reactive_cache_key(*args)) if Feature.enabled?(:reactive_caching_check_key_exists, default_enabled: true)
Rails.cache.exist?(alive_reactive_cache_key(*args))
else
!!Rails.cache.read(alive_reactive_cache_key(*args))
end
end end
def enqueuing_update(*args) def enqueuing_update(*args)
......
---
title: Allow ReactiveCaching to support nil value
merge_request: 30456
author:
type: fixed
...@@ -47,30 +47,12 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -47,30 +47,12 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
subject(:go!) { instance.result } subject(:go!) { instance.result }
context 'when cache is empty' do shared_examples 'a cacheable value' do |cached_value|
it { is_expected.to be_nil }
it 'enqueues a background worker to bootstrap the cache' do
expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666)
go!
end
it 'updates the cache lifespan' do
expect(reactive_cache_alive?(instance)).to be_falsy
go!
expect(reactive_cache_alive?(instance)).to be_truthy
end
end
context 'when the cache is full' do
before do before do
stub_reactive_cache(instance, 4) stub_reactive_cache(instance, cached_value)
end end
it { is_expected.to eq(4) } it { is_expected.to eq(cached_value) }
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)
...@@ -90,9 +72,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -90,9 +72,7 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end end
it { is_expected.to be_nil } it { is_expected.to be_nil }
end
context 'when cache was invalidated' do
it 'refreshes cache' do it 'refreshes cache' do
expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666) expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666)
...@@ -101,12 +81,34 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -101,12 +81,34 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end end
end end
context 'when cache contains non-nil but blank value' do context 'when cache is empty' do
before do it { is_expected.to be_nil }
stub_reactive_cache(instance, false)
it 'enqueues a background worker to bootstrap the cache' do
expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666)
go!
end end
it { is_expected.to eq(false) } it 'updates the cache lifespan' do
expect(reactive_cache_alive?(instance)).to be_falsy
go!
expect(reactive_cache_alive?(instance)).to be_truthy
end
end
context 'when the cache is full' do
it_behaves_like 'a cacheable value', 4
end
context 'when the cache contains non-nil but blank value' do
it_behaves_like 'a cacheable value', false
end
context 'when the cache contains nil value' do
it_behaves_like 'a cacheable value', nil
end end
end end
......
...@@ -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) unless data.nil? write_reactive_cache(subject, data, *qualifiers) unless subject.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