Skip to content

Commit

Permalink
support read_multi
Browse files Browse the repository at this point in the history
  • Loading branch information
LcpMarvel committed Dec 14, 2015
1 parent f562449 commit 6a175a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def fragment_cache(cached_hash, non_cached_hash)
private

def serializable_hash_for_collection(options)
if options[:batch_cache].blank? && ActiveModelSerializers.config.cache_store.present?
keys = CachedSerializer.object_cache_keys(serializer, @include_tree)
if keys.present?
values = ActiveModelSerializers.config.cache_store.read_multi(*keys)

options.merge!(batch_cache: values)
end
end

serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
end

Expand Down Expand Up @@ -56,6 +65,14 @@ def include_meta(json)
end

def resource_object_for(options)
if options[:batch_cache].present?
cache_key = CachedSerializer.new(serializer).cache_key

value = options[:batch_cache][cache_key]

return value if value.present?
end

cache_check(serializer) do
serializer.attributes(options[:fields])
end
Expand Down
28 changes: 28 additions & 0 deletions lib/active_model/serializer/adapter/cached_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ def object_cache_key
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

# collection_serializer with the include_tree
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
end

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
Expand Down
12 changes: 12 additions & 0 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def test_cache_digest_definition
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
end

def test_object_cache_keys
serializer = CollectionSerializer.new([@comment, @comment])
include_tree = IncludeTree.from_include_args('*')

actual = Serializer::Adapter::CachedSerializer.object_cache_keys(serializer, include_tree)

assert_equal actual.size, 6
assert actual.any? { |key| key == 'comment/1' }
assert actual.any? { |key| key =~ %r{post/post-\d+} }
assert actual.any? { |key| key =~ %r{writer/author-\d+} }
end

def test_serializer_file_path_on_nix
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
caller_line = "#{path}:1:in `<top (required)>'"
Expand Down

0 comments on commit 6a175a5

Please sign in to comment.