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 of
040a97b9e9
which was a squash of
f89ed71058

from pr 1346
This commit is contained in:
kevintyll
2015-11-25 12:31:32 -05:00
committed by Benjamin Fleischer
parent b73b780b79
commit ab6bd600e3
5 changed files with 91 additions and 28 deletions

View File

@@ -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

View File

@@ -8,6 +8,10 @@ module ActiveModelSerializers
ActiveModelSerializers::Adapter.register(subclass)
end
def self.name
to_s.demodulize
end
attr_reader :serializer, :instance_options
def initialize(serializer, options = {})
@@ -15,6 +19,10 @@ module ActiveModelSerializers
@instance_options = options
end
def name
self.class.name
end
def serializable_hash(_options = nil)
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end

View File

@@ -1,13 +1,16 @@
module ActiveModelSerializers
class CachedSerializer
UndefinedCacheKey = Class.new(StandardError)
def initialize(serializer)
@cached_serializer = serializer
@klass = @cached_serializer.class
return unless cached? && !@cached_serializer.object.respond_to?(:cache_key) && @klass._cache_key.blank?
fail(UndefinedCacheKey, "#{@cached_serializer.object} must define #cache_key, or the cache_key option must be passed into cache on #{@cached_serializer}")
end
def cache_check(adapter_instance)
if cached?
@klass._cache.fetch(cache_key, @klass._cache_options) do
@klass._cache.fetch(cache_key(adapter_instance), @klass._cache_options) do
yield
end
elsif fragment_cached?
@@ -25,21 +28,16 @@ module ActiveModelSerializers
@klass.fragment_cache_enabled?
end
def cache_key
def cache_key(adapter_instance)
return @cache_key if defined?(@cache_key)
parts = []
parts << object_cache_key
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
parts << @cached_serializer.cache_key
parts << adapter_instance.name.underscore
parts << @klass._cache_digest unless @klass._skip_digest?
@cache_key = parts.join('/')
end
def object_cache_key
object_time_safe = @cached_serializer.object.updated_at
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
@klass._cache_key ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
end
# find all cache_key for the collection_serializer
# @param collection_serializer
# @param include_tree