2016-12-17 04:09:50 +00:00
|
|
|
module ReactiveCachingHelpers
|
|
|
|
def reactive_cache_key(subject, *qualifiers)
|
|
|
|
([subject.class.reactive_cache_key.call(subject)].flatten + qualifiers).join(':')
|
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def alive_reactive_cache_key(subject, *qualifiers)
|
|
|
|
reactive_cache_key(subject, *(qualifiers + ['alive']))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_reactive_cache(subject = nil, data = nil, *qualifiers)
|
2016-12-17 04:09:50 +00:00
|
|
|
allow(ReactiveCachingWorker).to receive(:perform_async)
|
|
|
|
allow(ReactiveCachingWorker).to receive(:perform_in)
|
2017-01-12 22:31:02 +00:00
|
|
|
write_reactive_cache(subject, data, *qualifiers) if data
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2018-02-07 00:35:57 +00:00
|
|
|
def synchronous_reactive_cache(subject)
|
|
|
|
allow(service).to receive(:with_reactive_cache) do |*args, &block|
|
|
|
|
block.call(service.calculate_reactive_cache(*args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def read_reactive_cache(subject, *qualifiers)
|
|
|
|
Rails.cache.read(reactive_cache_key(subject, *qualifiers))
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def write_reactive_cache(subject, data, *qualifiers)
|
|
|
|
start_reactive_cache_lifetime(subject, *qualifiers)
|
|
|
|
Rails.cache.write(reactive_cache_key(subject, *qualifiers), data)
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def reactive_cache_alive?(subject, *qualifiers)
|
|
|
|
Rails.cache.read(alive_reactive_cache_key(subject, *qualifiers))
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def invalidate_reactive_cache(subject, *qualifiers)
|
|
|
|
Rails.cache.delete(alive_reactive_cache_key(subject, *qualifiers))
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2017-01-12 22:31:02 +00:00
|
|
|
def start_reactive_cache_lifetime(subject, *qualifiers)
|
|
|
|
Rails.cache.write(alive_reactive_cache_key(subject, *qualifiers), true)
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def expect_reactive_cache_update_queued(subject)
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(ReactiveCachingWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(subject.class.reactive_cache_refresh_interval, subject.class, subject.id)
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
end
|