Skip to content

Commit e049933

Browse files
committed
Restore cache_read_multi, though benchmark does not show improvement
Benchmark results: { "commit_hash": "33ae947", "version": "0.10.0.rc5", "rails_version": "4.2.6", "benchmark_run[environment]": "2.2.2p95", "runs": [ { "benchmark_type[category]": "caching on: caching serializers: gc off", "benchmark_run[result][iterations_per_second]": 677.638, "benchmark_run[result][total_allocated_objects_per_iteration]": 1795 }, { "benchmark_type[category]": "caching off: caching serializers: gc off", "benchmark_run[result][iterations_per_second]": 647.854, "benchmark_run[result][total_allocated_objects_per_iteration]": 1795 }, { "benchmark_type[category]": "caching on: non-caching serializers: gc off", "benchmark_run[result][iterations_per_second]": 936.023, "benchmark_run[result][total_allocated_objects_per_iteration]": 1252 }, { "benchmark_type[category]": "caching off: non-caching serializers: gc off", "benchmark_run[result][iterations_per_second]": 882.823, "benchmark_run[result][total_allocated_objects_per_iteration]": 1252 } }
1 parent 33ae947 commit e049933

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Metrics/ClassLength:
3030
Exclude:
3131
- test/**/*.rb
3232

33+
Metrics/ModuleLength:
34+
Max: 103 # TODO: Lower to 100
35+
3336
Metrics/CyclomaticComplexity:
3437
Max: 7 # TODO: Lower to 6
3538

lib/active_model/serializer/caching.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ module Caching
1717
# compress
1818
# force
1919
# race_condition_ttl
20-
# Passed to ::_cache as
21-
# serializer._cache.fetch(cache_key, @klass._cache_options)
20+
# Passed as second argument to serializer.cache_store.fetch(cache_key, self.class._cache_options)
2221
serializer.class_attribute :_cache_digest # @api private : Generated
2322
end
2423
end
@@ -208,8 +207,12 @@ def cached_attributes(fields, adapter_instance)
208207

209208
def cache_check(adapter_instance)
210209
if self.class.cache_enabled?
211-
self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do
212-
yield
210+
cached_attributes = instance_options[:cached_attributes] || {}
211+
key = cache_key(adapter_instance)
212+
cached_attributes.fetch(key) do
213+
self.class.cache_store.fetch(key, self.class._cache_options) do
214+
yield
215+
end
213216
end
214217
elsif self.class.fragment_cache_enabled?
215218
fetch_fragment_cache(adapter_instance)

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def serializable_hash(options = nil)
1919
private
2020

2121
def serializable_hash_for_collection(options)
22+
instance_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(serializer, self, @include_tree)
2223
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
2324
end
2425

0 commit comments

Comments
 (0)