Use RequestStore in CacheableAttributes.cached for greater performance

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2018-05-29 18:39:03 +02:00
parent a886532cc0
commit 4eda09e3fb
No known key found for this signature in database
GPG Key ID: 98DFFD1C0C62B70B
2 changed files with 14 additions and 1 deletions

View File

@ -25,7 +25,11 @@ module CacheableAttributes
end
def cached
retrieve_from_cache
if RequestStore.active?
RequestStore[:"#{name}_cached_attributes"] ||= retrieve_from_cache
else
retrieve_from_cache
end
end
def retrieve_from_cache

View File

@ -177,6 +177,15 @@ describe CacheableAttributes do
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
describe '.cached', :use_clean_rails_memory_store_caching do