mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
support read_multi
This commit is contained in:
@@ -5,6 +5,7 @@ module ActiveModel
|
||||
def initialize(serializer, options = {})
|
||||
super
|
||||
@include_tree = IncludeTree.from_include_args(options[:include] || '*')
|
||||
@cached_attributes = options[:cache_attributes] || {}
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
@@ -24,9 +25,38 @@ module ActiveModel
|
||||
private
|
||||
|
||||
def serializable_hash_for_collection(options)
|
||||
cache_attributes
|
||||
|
||||
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
|
||||
end
|
||||
|
||||
# Read cache from cache_store
|
||||
# @return [Hash]
|
||||
def cache_read_multi
|
||||
return {} if ActiveModelSerializers.config.cache_store.blank?
|
||||
|
||||
keys = CachedSerializer.object_cache_keys(serializer, @include_tree)
|
||||
|
||||
return {} if keys.blank?
|
||||
|
||||
ActiveModelSerializers.config.cache_store.read_multi(*keys)
|
||||
end
|
||||
|
||||
# Set @cached_attributes
|
||||
def cache_attributes
|
||||
return if @cached_attributes.present?
|
||||
|
||||
@cached_attributes = cache_read_multi
|
||||
end
|
||||
|
||||
# Get attributes from @cached_attributes
|
||||
# @return [Hash] cached attributes
|
||||
def cached_attributes(cached_serializer)
|
||||
return yield unless cached_serializer.cached?
|
||||
|
||||
@cached_attributes.fetch(cached_serializer.cache_key) { yield }
|
||||
end
|
||||
|
||||
def serializable_hash_for_single_resource(options)
|
||||
resource = resource_object_for(options)
|
||||
relationships = resource_relationships(options)
|
||||
@@ -56,8 +86,12 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def resource_object_for(options)
|
||||
cache_check(serializer) do
|
||||
serializer.attributes(options[:fields])
|
||||
cached_serializer = CachedSerializer.new(serializer)
|
||||
|
||||
cached_attributes(cached_serializer) do
|
||||
cached_serializer.cache_check(self) do
|
||||
serializer.attributes(options[:fields])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,10 +28,12 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def cache_key
|
||||
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.join('/')
|
||||
@cache_key = parts.join('/')
|
||||
end
|
||||
|
||||
def object_cache_key
|
||||
@@ -39,6 +41,38 @@ module ActiveModel
|
||||
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
|
||||
# @return [Array] all cache_key of collection_serializer
|
||||
def self.object_cache_keys(serializers, include_tree)
|
||||
cache_keys = []
|
||||
|
||||
serializers.each do |serializer|
|
||||
cache_keys << object_cache_key(serializer)
|
||||
|
||||
serializer.associations(include_tree).each do |association|
|
||||
if association.serializer.respond_to?(:each)
|
||||
association.serializer.each do |sub_serializer|
|
||||
cache_keys << object_cache_key(sub_serializer)
|
||||
end
|
||||
else
|
||||
cache_keys << object_cache_key(association.serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cache_keys.compact.uniq
|
||||
end
|
||||
|
||||
# @return [String, nil] the cache_key of the serializer or nil
|
||||
def self.object_cache_key(serializer)
|
||||
return unless serializer.present? && serializer.object.present?
|
||||
|
||||
cached_serializer = new(serializer)
|
||||
cached_serializer.cached? ? cached_serializer.cache_key : nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user