@@ -197,7 +197,7 @@ def object_cache_keys(collection_serializer, adapter_instance, include_directive
197197 def object_cache_key ( serializer , adapter_instance )
198198 return unless serializer . present? && serializer . object . present?
199199
200- serializer . class . cache_enabled? ? serializer . cache_key ( adapter_instance ) : nil
200+ ( serializer . class . cache_enabled? || serializer . class . fragment_cache_enabled? ) ? serializer . cache_key ( adapter_instance ) : nil
201201 end
202202 end
203203
@@ -211,7 +211,7 @@ def fetch_attributes(fields, cached_attributes, adapter_instance)
211211 end
212212 end
213213 elsif serializer_class . fragment_cache_enabled?
214- fetch_attributes_fragment ( adapter_instance )
214+ fetch_attributes_fragment ( adapter_instance , cached_attributes )
215215 else
216216 attributes ( fields , true )
217217 end
@@ -230,7 +230,8 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
230230 # 1. Determine cached fields from serializer class options
231231 # 2. Get non_cached_fields and fetch cache_fields
232232 # 3. Merge the two hashes using adapter_instance#fragment_cache
233- def fetch_attributes_fragment ( adapter_instance )
233+ # rubocop:disable Metrics/AbcSize
234+ def fetch_attributes_fragment ( adapter_instance , cached_attributes = { } )
234235 serializer_class . _cache_options ||= { }
235236 serializer_class . _cache_options [ :key ] = serializer_class . _cache_key if serializer_class . _cache_key
236237 fields = serializer_class . fragmented_attributes
@@ -243,15 +244,17 @@ def fetch_attributes_fragment(adapter_instance)
243244 cached_fields = fields [ :cached ] . dup
244245 key = cache_key ( adapter_instance )
245246 cached_hash =
246- serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
247- hash = attributes ( cached_fields , true )
248- include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
249- hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
247+ cached_attributes . fetch ( key ) do
248+ serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
249+ hash = attributes ( cached_fields , true )
250+ include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
251+ hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
252+ end
250253 end
251-
252254 # Merge both results
253255 adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
254256 end
257+ # rubocop:enable Metrics/AbcSize
255258
256259 def cache_key ( adapter_instance )
257260 return @cache_key if defined? ( @cache_key )
0 commit comments