@@ -197,7 +197,7 @@ def object_cache_keys(collection_serializer, adapter_instance, include_directive
197
197
def object_cache_key ( serializer , adapter_instance )
198
198
return unless serializer . present? && serializer . object . present?
199
199
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
201
201
end
202
202
end
203
203
@@ -211,7 +211,7 @@ def fetch_attributes(fields, cached_attributes, adapter_instance)
211
211
end
212
212
end
213
213
elsif serializer_class . fragment_cache_enabled?
214
- fetch_attributes_fragment ( adapter_instance )
214
+ fetch_attributes_fragment ( adapter_instance , cached_attributes )
215
215
else
216
216
attributes ( fields , true )
217
217
end
@@ -230,7 +230,8 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
230
230
# 1. Determine cached fields from serializer class options
231
231
# 2. Get non_cached_fields and fetch cache_fields
232
232
# 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 = { } )
234
235
serializer_class . _cache_options ||= { }
235
236
serializer_class . _cache_options [ :key ] = serializer_class . _cache_key if serializer_class . _cache_key
236
237
fields = serializer_class . fragmented_attributes
@@ -243,15 +244,17 @@ def fetch_attributes_fragment(adapter_instance)
243
244
cached_fields = fields [ :cached ] . dup
244
245
key = cache_key ( adapter_instance )
245
246
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
250
253
end
251
-
252
254
# Merge both results
253
255
adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
254
256
end
257
+ # rubocop:enable Metrics/AbcSize
255
258
256
259
def cache_key ( adapter_instance )
257
260
return @cache_key if defined? ( @cache_key )
0 commit comments