@@ -18,7 +18,7 @@ module Caching
1818 # race_condition_ttl
1919 # Passed to ::_cache as
2020 # 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)
2222 serializer . class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
2323 end
2424 end
@@ -203,23 +203,23 @@ def object_cache_key(serializer, adapter_instance)
203203
204204 ### INSTANCE METHODS
205205 def fetch_attributes ( fields , cached_attributes , adapter_instance )
206- if self . class . cache_enabled?
206+ if serializer_class . cache_enabled?
207207 key = cache_key ( adapter_instance )
208208 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
210210 attributes ( fields , true )
211211 end
212212 end
213- elsif self . class . fragment_cache_enabled?
213+ elsif serializer_class . fragment_cache_enabled?
214214 fetch_attributes_fragment ( adapter_instance )
215215 else
216216 attributes ( fields , true )
217217 end
218218 end
219219
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
223223 yield
224224 end
225225 else
@@ -231,9 +231,9 @@ def fetch(adapter_instance, cache_options = self.class._cache_options)
231231 # 2. Get non_cached_fields and fetch cache_fields
232232 # 3. Merge the two hashes using adapter_instance#fragment_cache
233233 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
237237
238238 non_cached_fields = fields [ :non_cached ] . dup
239239 non_cached_hash = attributes ( non_cached_fields , true )
@@ -243,7 +243,7 @@ def fetch_attributes_fragment(adapter_instance)
243243 cached_fields = fields [ :cached ] . dup
244244 key = cache_key ( adapter_instance )
245245 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
247247 hash = attributes ( cached_fields , true )
248248 include_directive = JSONAPI ::IncludeDirective . new ( cached_fields - hash . keys )
249249 hash . merge! resource_relationships ( { } , { include_directive : include_directive } , adapter_instance )
@@ -259,7 +259,7 @@ def cache_key(adapter_instance)
259259 parts = [ ]
260260 parts << object_cache_key
261261 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?
263263 @cache_key = parts . join ( '/' )
264264 end
265265
@@ -268,14 +268,18 @@ def cache_key(adapter_instance)
268268 def object_cache_key
269269 if object . respond_to? ( :cache_key )
270270 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 ] ) )
272272 object_time_safe = object . updated_at
273273 object_time_safe = object_time_safe . strftime ( '%Y%m%d%H%M%S%9N' ) if object_time_safe . respond_to? ( :strftime )
274274 "#{ serializer_cache_key } /#{ object . id } -#{ object_time_safe } "
275275 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'"
277277 end
278278 end
279+
280+ def serializer_class
281+ @serializer_class ||= self . class
282+ end
279283 end
280284 end
281285end
0 commit comments