Remove remaining fragmented cache class

This commit is contained in:
Benjamin Fleischer
2016-06-07 03:42:03 -05:00
parent 253205bb49
commit b8924157d7
4 changed files with 50 additions and 84 deletions

View File

@@ -194,10 +194,6 @@ module ActiveModel
def read_attribute_for_serialization(attr)
if respond_to?(attr)
send(attr)
elsif self.class._fragmented
# Attribute method wasn't available on this (fragment cached) serializer,
# so read it from the original serializer it was based on.
self.class._fragmented.read_attribute_for_serialization(attr)
else
object.read_attribute_for_serialization(attr)
end

View File

@@ -7,7 +7,6 @@ module ActiveModel
included do
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 : Used ONLY by FragmentedSerializer to reference original serializer
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 fetch_attributes. Cannot combine with except
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists fetch_attributes. Cannot combine with only
@@ -69,12 +68,15 @@ module ActiveModel
_cache_options && _cache_options[:skip_digest]
end
def cached_attributes
_cache_only ? _cache_only : _attributes - _cache_except
end
def non_cached_attributes
_attributes - cached_attributes
def fragmented_attributes
cached = _cache_only ? _cache_only : _attributes - _cache_except
cached = cached.map! {|field| _attributes_keys.fetch(field, field) }
non_cached = _attributes - cached
non_cached = non_cached.map! {|field| _attributes_keys.fetch(field, field) }
{
cached: cached,
non_cached: non_cached
}
end
# Enables a serializer to be automatically cached
@@ -205,13 +207,13 @@ module ActiveModel
key = cache_key(adapter_instance)
cached_attributes.fetch(key) do
self.class.cache_store.fetch(key, self.class._cache_options) do
attributes(fields)
attributes(fields, true)
end
end
elsif self.class.fragment_cache_enabled?
fetch_fragment_cache(adapter_instance)
fetch_attributes_fragment(adapter_instance)
else
attributes(fields)
attributes(fields, true)
end
end
@@ -225,74 +227,40 @@ module ActiveModel
end
end
# 1. Create a CachedSerializer from the serializer class
# 2. Serialize the above two with the given adapter
# 3. Pass their serializations to the adapter +::fragment_cache+
#
# It will split the serializer into two, one that will be cached and one that will not
#
# Given a resource name
# 1. Dynamically creates a CachedSerializer
# for a given class 'name'
# 2. Call
# CachedSerializer.cache(serializer._cache_options)
# CachedSerializer._fragmented = serializer
# 3. Build a hash keyed to the +cached+ and +non_cached+ serializers
# 4. Call +cached_attributes+ on the serializer class and the above hash
# 5. Return the hash
#
# @example
# When +name+ is <tt>User::Admin</tt>
# creates the Serializer classes (if they don't exist).
# CachedUser_AdminSerializer
#
# Given a hash of its cached and non-cached serializers
# 1. Determine cached attributes from serializer class options
# 2. Add cached attributes to cached Serializer
# 3. Add non-cached attributes to non-cached Serializer
def fetch_fragment_cache(adapter_instance)
# 1. Determine cached fields from serializer class options
# 2. Get non_cached_fields and fetch cache_fields
# 3. Merge the two hashes using adapter_instance#fragment_cache
def fetch_attributes_fragment(adapter_instance)
self.class._cache_options ||= {}
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
options = { include_directive: ActiveModel::Serializer.include_directive_from_options({})}
fields = self.class.fragmented_attributes
cached_serializer = _get_or_create_fragment_cached_serializer
cached_hash = ActiveModelSerializers::SerializableResource.new(
object,
serializer: cached_serializer,
adapter: adapter_instance.class
).serializable_hash
non_cached_fields = fields[:non_cached].dup
non_cached_hash = attributes(non_cached_fields, true)
(non_cached_fields - non_cached_hash.keys).each do |missing_field|
# TODO: use _attributes_keys?
# gets any other associations, etc.
non_cached_hash[missing_field] = read_attribute_for_serialization(missing_field)
end
fields = self.class.non_cached_attributes
non_cached_hash = attributes(fields, true)
cached_fields = fields[:cached].dup
key = cache_key(adapter_instance)
cached_hash =
self.class.cache_store.fetch(key, self.class._cache_options) do
hash = attributes(cached_fields, true)
(cached_fields - hash.keys).each do |missing_field|
# TODO: use _attributes_keys?
# gets any other associations, etc.
hash[missing_field] = read_attribute_for_serialization(missing_field)
end
hash
end
# Merge both results
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
end
def _get_or_create_fragment_cached_serializer
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
cached_serializer_name = "Cached#{serializer_class_name}"
if Object.const_defined?(cached_serializer_name)
cached_serializer = Object.const_get(cached_serializer_name)
# HACK: Test concern in production code :(
# But it's better than running all the cached fragment serializer
# code multiple times.
if Rails.env == 'test'.freeze
Object.send(:remove_const, cached_serializer_name)
return _get_or_create_fragment_cached_serializer
end
return cached_serializer
end
cached_serializer = Object.const_set(cached_serializer_name, Class.new(ActiveModel::Serializer))
cached_serializer.cache(self.class._cache_options)
cached_serializer.type(self.class._type)
cached_serializer._fragmented = self
self.class.cached_attributes.each do |attribute|
options = self.class._attributes_keys[attribute] || {}
cached_serializer.attribute(attribute, options)
end
cached_serializer
end
def cache_key(adapter_instance)
return @cache_key if defined?(@cache_key)