Skip to content

Commit ab1b06c

Browse files
committed
refactor batch_cache code
1 parent 6a175a5 commit ab1b06c

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

lib/active_model/serializer/adapter/attributes.rb

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,31 @@ def fragment_cache(cached_hash, non_cached_hash)
2424
private
2525

2626
def serializable_hash_for_collection(options)
27-
if options[:batch_cache].blank? && ActiveModelSerializers.config.cache_store.present?
28-
keys = CachedSerializer.object_cache_keys(serializer, @include_tree)
29-
if keys.present?
30-
values = ActiveModelSerializers.config.cache_store.read_multi(*keys)
31-
32-
options.merge!(batch_cache: values)
33-
end
34-
end
27+
options.merge!(batch_cache(options))
3528

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

32+
# Read cache from cache_store, and set those into options[:batch_cache]
33+
# @return [Hash] a batch of cache
34+
def batch_cache(options)
35+
return {} if options[:batch_cache].present? || ActiveModelSerializers.config.cache_store.blank?
36+
37+
keys = CachedSerializer.object_cache_keys(serializer, @include_tree)
38+
39+
return {} if keys.blank?
40+
41+
{ batch_cache: ActiveModelSerializers.config.cache_store.read_multi(*keys) }
42+
end
43+
44+
# Get serializer from options[:batch_cache].
45+
# @return [Hash] cached attributes
46+
def batch_cached_serializer(options)
47+
return unless options[:batch_cache].present?
48+
49+
options[:batch_cache][CachedSerializer.new(serializer).cache_key]
50+
end
51+
3952
def serializable_hash_for_single_resource(options)
4053
resource = resource_object_for(options)
4154
relationships = resource_relationships(options)
@@ -65,13 +78,9 @@ def include_meta(json)
6578
end
6679

6780
def resource_object_for(options)
68-
if options[:batch_cache].present?
69-
cache_key = CachedSerializer.new(serializer).cache_key
70-
71-
value = options[:batch_cache][cache_key]
81+
batch_cached_serializer_attributes = batch_cached_serializer(options)
7282

73-
return value if value.present?
74-
end
83+
return batch_cached_serializer_attributes if batch_cached_serializer_attributes.present?
7584

7685
cache_check(serializer) do
7786
serializer.attributes(options[:fields])

lib/active_model/serializer/adapter/cached_serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def object_cache_key
4040
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
4141
end
4242

43-
# collection_serializer with the include_tree
43+
# find all cache_key for the collection_serializer
44+
# @param collection_serializer
45+
# @param include_tree
46+
# @return [Array] all cache_key of collection_serializer
4447
def self.object_cache_keys(serializers, include_tree)
4548
cache_keys = []
4649

@@ -61,6 +64,7 @@ def self.object_cache_keys(serializers, include_tree)
6164
cache_keys.compact
6265
end
6366

67+
# @return [String, nil] the cache_key of the serializer or nil
6468
def self.object_cache_key(serializer)
6569
return unless serializer.present? && serializer.object.present?
6670

test/serializers/cache_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ def test_object_cache_keys
161161
assert actual.any? { |key| key =~ %r{writer/author-\d+} }
162162
end
163163

164+
def test_batch_cache
165+
serializer = CollectionSerializer.new([@comment, @comment])
166+
167+
Timecop.freeze(Time.now) do
168+
render_object_with_cache(@comment)
169+
170+
batch_cache = ActiveModel::Serializer::Adapter::Attributes.new(serializer).send(:batch_cache, {})[:batch_cache]
171+
172+
assert_equal batch_cache[@comment.cache_key], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
173+
assert_equal batch_cache[@comment.post.cache_key], Post.new(id: "post", title: 'New Post', body: 'Body').attributes
174+
175+
writer = @comment.post.blog.writer
176+
writer_cache_key = "writer/#{writer.id}-#{writer.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
177+
178+
assert_equal batch_cache[writer_cache_key], Author.new(id: "author", name: 'Joao M. D. Moura').attributes
179+
end
180+
end
181+
164182
def test_serializer_file_path_on_nix
165183
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
166184
caller_line = "#{path}:1:in `<top (required)>'"

0 commit comments

Comments
 (0)