Skip to content

Commit

Permalink
refactor batch_cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
LcpMarvel committed Dec 16, 2015
1 parent 6a175a5 commit eabafb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
18 changes: 18 additions & 0 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<top (required)>'"
Expand Down

0 comments on commit eabafb2

Please sign in to comment.