From bcf335852424ed4941e4b6ba281ae610d3bb8c15 Mon Sep 17 00:00:00 2001 From: zaaroth Date: Thu, 23 Jun 2016 00:33:02 -0300 Subject: [PATCH] Ensuring read_multi works with fragment cache. (#1814) * Ensuring read_multi works with fragment cache. --- CHANGELOG.md | 1 + lib/active_model/serializer/caching.rb | 19 +++++++++-------- test/cache_test.rb | 28 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 701b4571..6632cc3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Breaking changes: Features: Fixes: +- [#1814] (https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache Misc: diff --git a/lib/active_model/serializer/caching.rb b/lib/active_model/serializer/caching.rb index dfef8f02..20362ce8 100644 --- a/lib/active_model/serializer/caching.rb +++ b/lib/active_model/serializer/caching.rb @@ -197,7 +197,7 @@ module ActiveModel def object_cache_key(serializer, adapter_instance) return unless serializer.present? && serializer.object.present? - serializer.class.cache_enabled? ? serializer.cache_key(adapter_instance) : nil + (serializer.class.cache_enabled? || serializer.class.fragment_cache_enabled?) ? serializer.cache_key(adapter_instance) : nil end end @@ -211,7 +211,7 @@ module ActiveModel end end elsif serializer_class.fragment_cache_enabled? - fetch_attributes_fragment(adapter_instance) + fetch_attributes_fragment(adapter_instance, cached_attributes) else attributes(fields, true) end @@ -230,7 +230,8 @@ module ActiveModel # 1. Determine cached fields from serializer class options # 2. Get non_cached_fields and fetch cache_fields # 3. Merge the two hashes using adapter_instance#fragment_cache - def fetch_attributes_fragment(adapter_instance) + # rubocop:disable Metrics/AbcSize + def fetch_attributes_fragment(adapter_instance, cached_attributes = {}) serializer_class._cache_options ||= {} serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key fields = serializer_class.fragmented_attributes @@ -243,15 +244,17 @@ module ActiveModel cached_fields = fields[:cached].dup key = cache_key(adapter_instance) cached_hash = - serializer_class.cache_store.fetch(key, serializer_class._cache_options) do - hash = attributes(cached_fields, true) - include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys) - hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance) + cached_attributes.fetch(key) do + serializer_class.cache_store.fetch(key, serializer_class._cache_options) do + hash = attributes(cached_fields, true) + include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys) + hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance) + end end - # Merge both results adapter_instance.fragment_cache(cached_hash, non_cached_hash) end + # rubocop:enable Metrics/AbcSize def cache_key(adapter_instance) return @cache_key if defined?(@cache_key) diff --git a/test/cache_test.rb b/test/cache_test.rb index 57312a91..73d59bbd 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -321,6 +321,34 @@ module ActiveModelSerializers end end + def test_cache_read_multi_with_fragment_cache_enabled + post_serializer = Class.new(ActiveModel::Serializer) do + cache except: [:body] + end + + serializers = ActiveModel::Serializer::CollectionSerializer.new([@post, @post], serializer: post_serializer) + + Timecop.freeze(Time.current) do + # Warming up. + options = {} + adapter_options = {} + adapter_instance = ActiveModelSerializers::Adapter::Attributes.new(serializers, adapter_options) + serializers.serializable_hash(adapter_options, options, adapter_instance) + + # Should find something with read_multi now + adapter_options = {} + serializers.serializable_hash(adapter_options, options, adapter_instance) + cached_attributes = adapter_options.fetch(:cached_attributes) + + include_directive = ActiveModelSerializers.default_include_directive + manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive) + + refute_equal 0, cached_attributes.size + refute_equal 0, manual_cached_attributes.size + assert_equal manual_cached_attributes, cached_attributes + end + end + def test_serializer_file_path_on_nix path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb' caller_line = "#{path}:1:in `'"