Commit c807a418 authored by Shinya Maeda's avatar Shinya Maeda

Backport reactive caching

parent 6cb30f83
...@@ -59,6 +59,9 @@ module ReactiveCaching ...@@ -59,6 +59,9 @@ module ReactiveCaching
raise NotImplementedError raise NotImplementedError
end end
def reactive_cache_updated(*args)
end
def with_reactive_cache(*args, &blk) def with_reactive_cache(*args, &blk)
bootstrap = !within_reactive_cache_lifetime?(*args) bootstrap = !within_reactive_cache_lifetime?(*args)
Rails.cache.write(alive_reactive_cache_key(*args), true, expires_in: self.class.reactive_cache_lifetime) Rails.cache.write(alive_reactive_cache_key(*args), true, expires_in: self.class.reactive_cache_lifetime)
...@@ -81,8 +84,11 @@ module ReactiveCaching ...@@ -81,8 +84,11 @@ module ReactiveCaching
locking_reactive_cache(*args) do locking_reactive_cache(*args) do
if within_reactive_cache_lifetime?(*args) if within_reactive_cache_lifetime?(*args)
enqueuing_update(*args) do enqueuing_update(*args) do
value = calculate_reactive_cache(*args) key = full_reactive_cache_key(*args)
Rails.cache.write(full_reactive_cache_key(*args), value) new_value = calculate_reactive_cache(*args)
old_value = Rails.cache.read(key)
Rails.cache.write(key, new_value)
reactive_cache_updated(*args) if new_value != old_value
end end
end end
end end
......
...@@ -125,6 +125,13 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -125,6 +125,13 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
go! go!
end end
it "calls a reactive_cache_updated only once if content did not change on subsequent update" do
expect(instance).to receive(:calculate_reactive_cache).twice
expect(instance).to receive(:reactive_cache_updated).once
2.times { instance.exclusively_update_reactive_cache! }
end
context 'and #calculate_reactive_cache raises an exception' do context 'and #calculate_reactive_cache raises an exception' do
before do before do
stub_reactive_cache(instance, "preexisting") stub_reactive_cache(instance, "preexisting")
......
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