2016-12-16 23:09:50 -05:00
|
|
|
module ReactiveCachingHelpers
|
|
|
|
def reactive_cache_key(subject, *qualifiers)
|
|
|
|
([subject.class.reactive_cache_key.call(subject)].flatten + qualifiers).join(':')
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05: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-16 23:09:50 -05:00
|
|
|
allow(ReactiveCachingWorker).to receive(:perform_async)
|
|
|
|
allow(ReactiveCachingWorker).to receive(:perform_in)
|
2017-01-12 17:31:02 -05:00
|
|
|
write_reactive_cache(subject, data, *qualifiers) if data
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def read_reactive_cache(subject, *qualifiers)
|
|
|
|
Rails.cache.read(reactive_cache_key(subject, *qualifiers))
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05: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-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def reactive_cache_alive?(subject, *qualifiers)
|
|
|
|
Rails.cache.read(alive_reactive_cache_key(subject, *qualifiers))
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def invalidate_reactive_cache(subject, *qualifiers)
|
|
|
|
Rails.cache.delete(alive_reactive_cache_key(subject, *qualifiers))
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def start_reactive_cache_lifetime(subject, *qualifiers)
|
|
|
|
Rails.cache.write(alive_reactive_cache_key(subject, *qualifiers), true)
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def expect_reactive_cache_update_queued(subject)
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(ReactiveCachingWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(subject.class.reactive_cache_refresh_interval, subject.class, subject.id)
|
2016-12-16 23:09:50 -05:00
|
|
|
end
|
|
|
|
end
|