Commit 4eda09e3 authored by Rémy Coutable's avatar Rémy Coutable

Use RequestStore in CacheableAttributes.cached for greater performance

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent a886532c
...@@ -25,8 +25,12 @@ module CacheableAttributes ...@@ -25,8 +25,12 @@ module CacheableAttributes
end end
def cached def cached
if RequestStore.active?
RequestStore[:"#{name}_cached_attributes"] ||= retrieve_from_cache
else
retrieve_from_cache retrieve_from_cache
end end
end
def retrieve_from_cache def retrieve_from_cache
record = Rails.cache.read(cache_key) record = Rails.cache.read(cache_key)
......
...@@ -177,6 +177,15 @@ describe CacheableAttributes do ...@@ -177,6 +177,15 @@ describe CacheableAttributes do
end end
end end
end end
it 'uses RequestStore in addition to Rails.cache', :request_store do
# Warm up the cache
create(:application_setting).cache!
expect(Rails.cache).to receive(:read).with(ApplicationSetting.cache_key).once.and_call_original
2.times { ApplicationSetting.current }
end
end end
describe '.cached', :use_clean_rails_memory_store_caching do describe '.cached', :use_clean_rails_memory_store_caching do
......
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