mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
When caching, return the object's cache_key up front if it's defined.
This will prevent objects PORO objects that don't have updated_at defined, from throwing an error. Not as big a deal now that PORO objects can inherit ActiveModelSerializers::Model, but still necessary if it's not inherited for whatever reason. Add the Adapter type to the cache key. This prevents incorrect results when the same object is serialized with different adapters. BF: Cherry-pick of040a97b9e9which was a squash off89ed71058from pr 1346
This commit is contained in:
committed by
Benjamin Fleischer
parent
b73b780b79
commit
ab6bd600e3
@@ -7,7 +7,7 @@ module ActiveModel
|
||||
with_options instance_writer: false, instance_reader: false do |serializer|
|
||||
serializer.class_attribute :_cache # @api private : the cache store
|
||||
serializer.class_attribute :_fragmented # @api private : @see ::fragmented
|
||||
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key
|
||||
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key.
|
||||
serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists cached_attributes. Cannot combine with except
|
||||
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists cached_attributes. Cannot combine with only
|
||||
serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
|
||||
@@ -58,6 +58,10 @@ module ActiveModel
|
||||
''.freeze
|
||||
end
|
||||
|
||||
def _skip_digest?
|
||||
_cache_options && _cache_options[:skip_digest]
|
||||
end
|
||||
|
||||
# @api private
|
||||
# Used by FragmentCache on the CachedSerializer
|
||||
# to call attribute methods on the fragmented cached serializer.
|
||||
@@ -142,6 +146,18 @@ module ActiveModel
|
||||
(_cache_only && !_cache_except || !_cache_only && _cache_except)
|
||||
end
|
||||
end
|
||||
|
||||
# Use object's cache_key if available, else derive a key from the object
|
||||
# Pass the `key` option to the `cache` declaration or override this method to customize the cache key
|
||||
def cache_key
|
||||
if object.respond_to?(:cache_key)
|
||||
object.cache_key
|
||||
else
|
||||
object_time_safe = object.updated_at
|
||||
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
||||
"#{self.class._cache_key}/#{object.id}-#{object_time_safe}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user