@@ -170,6 +170,7 @@ def fragment_cache_enabled?
170
170
171
171
# Read cache from cache_store
172
172
# @return [Hash]
173
+ # Used in CollectionSerializer to set :cached_attributes
173
174
def cache_read_multi ( collection_serializer , adapter_instance , include_directive )
174
175
return { } if ActiveModelSerializers . config . cache_store . blank?
175
176
@@ -215,23 +216,17 @@ def object_cache_key(serializer, adapter_instance)
215
216
216
217
### INSTANCE METHODS
217
218
def fetch_attributes ( fields , cached_attributes , adapter_instance )
218
- if serializer_class . cache_enabled?
219
- key = cache_key ( adapter_instance )
220
- cached_attributes . fetch ( key ) do
221
- serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
222
- attributes ( fields , true )
223
- end
219
+ key = cache_key ( adapter_instance )
220
+ cached_attributes . fetch ( key ) do
221
+ fetch ( adapter_instance , serializer_class . _cache_options , key ) do
222
+ attributes ( fields , true )
224
223
end
225
- elsif serializer_class . fragment_cache_enabled?
226
- fetch_attributes_fragment ( adapter_instance , cached_attributes )
227
- else
228
- attributes ( fields , true )
229
224
end
230
225
end
231
226
232
- def fetch ( adapter_instance , cache_options = serializer_class . _cache_options )
227
+ def fetch ( adapter_instance , cache_options = serializer_class . _cache_options , key = cache_key ( adapter_instance ) )
233
228
if serializer_class . cache_store
234
- serializer_class . cache_store . fetch ( cache_key ( adapter_instance ) , cache_options ) do
229
+ serializer_class . cache_store . fetch ( key , cache_options ) do
235
230
yield
236
231
end
237
232
else
@@ -242,7 +237,6 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
242
237
# 1. Determine cached fields from serializer class options
243
238
# 2. Get non_cached_fields and fetch cache_fields
244
239
# 3. Merge the two hashes using adapter_instance#fragment_cache
245
- # rubocop:disable Metrics/AbcSize
246
240
def fetch_attributes_fragment ( adapter_instance , cached_attributes = { } )
247
241
serializer_class . _cache_options ||= { }
248
242
serializer_class . _cache_options [ :key ] = serializer_class . _cache_key if serializer_class . _cache_key
@@ -257,7 +251,7 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
257
251
key = cache_key ( adapter_instance )
258
252
cached_hash =
259
253
cached_attributes . fetch ( key ) do
260
- serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
254
+ fetch ( adapter_instance , serializer_class . _cache_options , key ) do
261
255
hash = attributes ( cached_fields , true )
262
256
include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
263
257
hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
@@ -266,7 +260,6 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
266
260
# Merge both results
267
261
adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
268
262
end
269
- # rubocop:enable Metrics/AbcSize
270
263
271
264
def cache_key ( adapter_instance )
272
265
return @cache_key if defined? ( @cache_key )
0 commit comments