@@ -170,6 +170,7 @@ def fragment_cache_enabled?
170170
171171 # Read cache from cache_store
172172 # @return [Hash]
173+ # Used in CollectionSerializer to set :cached_attributes
173174 def cache_read_multi ( collection_serializer , adapter_instance , include_directive )
174175 return { } if ActiveModelSerializers . config . cache_store . blank?
175176
@@ -215,23 +216,17 @@ def object_cache_key(serializer, adapter_instance)
215216
216217 ### INSTANCE METHODS
217218 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 )
224223 end
225- elsif serializer_class . fragment_cache_enabled?
226- fetch_attributes_fragment ( adapter_instance , cached_attributes )
227- else
228- attributes ( fields , true )
229224 end
230225 end
231226
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 ) )
233228 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
235230 yield
236231 end
237232 else
@@ -242,7 +237,6 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
242237 # 1. Determine cached fields from serializer class options
243238 # 2. Get non_cached_fields and fetch cache_fields
244239 # 3. Merge the two hashes using adapter_instance#fragment_cache
245- # rubocop:disable Metrics/AbcSize
246240 def fetch_attributes_fragment ( adapter_instance , cached_attributes = { } )
247241 serializer_class . _cache_options ||= { }
248242 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 = {})
257251 key = cache_key ( adapter_instance )
258252 cached_hash =
259253 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
261255 hash = attributes ( cached_fields , true )
262256 include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
263257 hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
@@ -266,7 +260,6 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
266260 # Merge both results
267261 adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
268262 end
269- # rubocop:enable Metrics/AbcSize
270263
271264 def cache_key ( adapter_instance )
272265 return @cache_key if defined? ( @cache_key )
0 commit comments