@@ -18,7 +18,7 @@ module Caching
18
18
# race_condition_ttl
19
19
# Passed to ::_cache as
20
20
# serializer.cache_store.fetch(cache_key, @klass._cache_options)
21
- # Passed as second argument to serializer.cache_store.fetch(cache_key, self.class ._cache_options)
21
+ # Passed as second argument to serializer.cache_store.fetch(cache_key, serializer_class ._cache_options)
22
22
serializer . class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
23
23
end
24
24
end
@@ -203,23 +203,23 @@ def object_cache_key(serializer, adapter_instance)
203
203
204
204
### INSTANCE METHODS
205
205
def fetch_attributes ( fields , cached_attributes , adapter_instance )
206
- if self . class . cache_enabled?
206
+ if serializer_class . cache_enabled?
207
207
key = cache_key ( adapter_instance )
208
208
cached_attributes . fetch ( key ) do
209
- self . class . cache_store . fetch ( key , self . class . _cache_options ) do
209
+ serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
210
210
attributes ( fields , true )
211
211
end
212
212
end
213
- elsif self . class . fragment_cache_enabled?
213
+ elsif serializer_class . fragment_cache_enabled?
214
214
fetch_attributes_fragment ( adapter_instance )
215
215
else
216
216
attributes ( fields , true )
217
217
end
218
218
end
219
219
220
- def fetch ( adapter_instance , cache_options = self . class . _cache_options )
221
- if self . class . cache_store
222
- self . class . cache_store . fetch ( cache_key ( adapter_instance ) , cache_options ) do
220
+ def fetch ( adapter_instance , cache_options = serializer_class . _cache_options )
221
+ if serializer_class . cache_store
222
+ serializer_class . cache_store . fetch ( cache_key ( adapter_instance ) , cache_options ) do
223
223
yield
224
224
end
225
225
else
@@ -231,9 +231,9 @@ def fetch(adapter_instance, cache_options = self.class._cache_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
233
def fetch_attributes_fragment ( adapter_instance )
234
- self . class . _cache_options ||= { }
235
- self . class . _cache_options [ :key ] = self . class . _cache_key if self . class . _cache_key
236
- fields = self . class . fragmented_attributes
234
+ serializer_class . _cache_options ||= { }
235
+ serializer_class . _cache_options [ :key ] = serializer_class . _cache_key if serializer_class . _cache_key
236
+ fields = serializer_class . fragmented_attributes
237
237
238
238
non_cached_fields = fields [ :non_cached ] . dup
239
239
non_cached_hash = attributes ( non_cached_fields , true )
@@ -243,7 +243,7 @@ def fetch_attributes_fragment(adapter_instance)
243
243
cached_fields = fields [ :cached ] . dup
244
244
key = cache_key ( adapter_instance )
245
245
cached_hash =
246
- self . class . cache_store . fetch ( key , self . class . _cache_options ) do
246
+ serializer_class . cache_store . fetch ( key , serializer_class . _cache_options ) do
247
247
hash = attributes ( cached_fields , true )
248
248
include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
249
249
hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
@@ -259,7 +259,7 @@ def cache_key(adapter_instance)
259
259
parts = [ ]
260
260
parts << object_cache_key
261
261
parts << adapter_instance . cache_key
262
- parts << self . class . _cache_digest unless self . class . _skip_digest?
262
+ parts << serializer_class . _cache_digest unless serializer_class . _skip_digest?
263
263
@cache_key = parts . join ( '/' )
264
264
end
265
265
@@ -268,14 +268,18 @@ def cache_key(adapter_instance)
268
268
def object_cache_key
269
269
if object . respond_to? ( :cache_key )
270
270
object . cache_key
271
- elsif ( serializer_cache_key = ( self . class . _cache_key || self . class . _cache_options [ :key ] ) )
271
+ elsif ( serializer_cache_key = ( serializer_class . _cache_key || serializer_class . _cache_options [ :key ] ) )
272
272
object_time_safe = object . updated_at
273
273
object_time_safe = object_time_safe . strftime ( '%Y%m%d%H%M%S%9N' ) if object_time_safe . respond_to? ( :strftime )
274
274
"#{ serializer_cache_key } /#{ object . id } -#{ object_time_safe } "
275
275
else
276
- fail UndefinedCacheKey , "#{ object . class } must define #cache_key, or the 'key:' option must be passed into '#{ self . class } .cache'"
276
+ fail UndefinedCacheKey , "#{ object . class } must define #cache_key, or the 'key:' option must be passed into '#{ serializer_class } .cache'"
277
277
end
278
278
end
279
+
280
+ def serializer_class
281
+ @serializer_class ||= self . class
282
+ end
279
283
end
280
284
end
281
285
end
0 commit comments