From eabafb20b9ccd033e3cf7db411b8623bf9617141 Mon Sep 17 00:00:00 2001 From: lcp Date: Tue, 15 Dec 2015 12:52:57 +0800 Subject: [PATCH] refactor batch_cache code --- .../serializer/adapter/attributes.rb | 37 ++++++++++++------- test/serializers/cache_test.rb | 18 +++++++++ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/active_model/serializer/adapter/attributes.rb b/lib/active_model/serializer/adapter/attributes.rb index 52fec33fd..1a274a853 100644 --- a/lib/active_model/serializer/adapter/attributes.rb +++ b/lib/active_model/serializer/adapter/attributes.rb @@ -24,18 +24,31 @@ 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 + options.merge!(batch_cache(options)) serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) } end + # Read cache from cache_store, and set those into options[:batch_cache] + # @return [Hash] a batch of cache + def batch_cache(options) + return {} if options[:batch_cache].present? || ActiveModelSerializers.config.cache_store.blank? + + keys = CachedSerializer.object_cache_keys(serializer, @include_tree) + + return {} if keys.blank? + + { batch_cache: ActiveModelSerializers.config.cache_store.read_multi(*keys) } + end + + # Get serializer from options[:batch_cache]. + # @return [Hash] cached attributes + def batch_cached_serializer(options) + return unless options[:batch_cache].present? + + options[:batch_cache][CachedSerializer.new(serializer).cache_key] + end + def serializable_hash_for_single_resource(options) resource = resource_object_for(options) relationships = resource_relationships(options) @@ -65,13 +78,9 @@ 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] + batch_cached_serializer_attributes = batch_cached_serializer(options) - return value if value.present? - end + return batch_cached_serializer_attributes if batch_cached_serializer_attributes.present? cache_check(serializer) do serializer.attributes(options[:fields]) diff --git a/test/serializers/cache_test.rb b/test/serializers/cache_test.rb index 0cd9a6741..5ac2bcd18 100644 --- a/test/serializers/cache_test.rb +++ b/test/serializers/cache_test.rb @@ -161,6 +161,24 @@ def test_object_cache_keys assert actual.any? { |key| key =~ %r{writer/author-\d+} } end + def test_batch_cache + serializer = CollectionSerializer.new([@comment, @comment]) + + Timecop.freeze(Time.now) do + render_object_with_cache(@comment) + + batch_cache = ActiveModel::Serializer::Adapter::Attributes.new(serializer).send(:batch_cache, {})[:batch_cache] + + assert_equal batch_cache[@comment.cache_key], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes + assert_equal batch_cache[@comment.post.cache_key], Post.new(id: "post", title: 'New Post', body: 'Body').attributes + + writer = @comment.post.blog.writer + writer_cache_key = "writer/#{writer.id}-#{writer.updated_at.strftime("%Y%m%d%H%M%S%9N")}" + + assert_equal batch_cache[writer_cache_key], Author.new(id: "author", name: 'Joao M. D. Moura').attributes + end + 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 `'"