@@ -17,13 +17,13 @@ def cache_collection_render(instrumentation_payload, view, template)
17
17
# Result is a hash with the key represents the
18
18
# key used for cache lookup and the value is the item
19
19
# on which the partial is being rendered
20
- keyed_collection = collection_by_cache_keys ( view , template )
20
+ keyed_collection , ordered_keys = collection_by_cache_keys ( view , template )
21
21
22
22
# Pull all partials from cache
23
23
# Result is a hash, key matches the entry in
24
24
# `keyed_collection` where the cache was retrieved and the
25
25
# value is the value that was present in the cache
26
- cached_partials = collection_cache . read_multi ( *keyed_collection . keys )
26
+ cached_partials = collection_cache . read_multi ( *keyed_collection . keys )
27
27
instrumentation_payload [ :cache_hits ] = cached_partials . size
28
28
29
29
# Extract the items for the keys that are not found
@@ -40,11 +40,15 @@ def cache_collection_render(instrumentation_payload, view, template)
40
40
rendered_partials = @collection . empty? ? [ ] : yield
41
41
42
42
index = 0
43
- fetch_or_cache_partial ( cached_partials , template , order_by : keyed_collection . each_key ) do
43
+ keyed_partials = fetch_or_cache_partial ( cached_partials , template , order_by : keyed_collection . each_key ) do
44
44
# This block is called once
45
45
# for every cache miss while preserving order.
46
46
rendered_partials [ index ] . tap { index += 1 }
47
47
end
48
+
49
+ ordered_keys . map do |key |
50
+ keyed_partials [ key ]
51
+ end
48
52
end
49
53
50
54
def callable_cache_key?
@@ -56,8 +60,10 @@ def collection_by_cache_keys(view, template)
56
60
57
61
digest_path = view . digest_path_from_template ( template )
58
62
59
- @collection . each_with_object ( { } ) do |item , hash |
60
- hash [ expanded_cache_key ( seed . call ( item ) , view , template , digest_path ) ] = item
63
+ @collection . each_with_object ( [ { } , [ ] ] ) do |item , ( hash , ordered_keys ) |
64
+ key = expanded_cache_key ( seed . call ( item ) , view , template , digest_path )
65
+ ordered_keys << key
66
+ hash [ key ] = item
61
67
end
62
68
end
63
69
@@ -82,15 +88,16 @@ def expanded_cache_key(key, view, template, digest_path)
82
88
# If the partial is not already cached it will also be
83
89
# written back to the underlying cache store.
84
90
def fetch_or_cache_partial ( cached_partials , template , order_by :)
85
- order_by . map do |cache_key |
86
- if content = cached_partials [ cache_key ]
87
- build_rendered_template ( content , template )
88
- else
89
- yield . tap do |rendered_partial |
90
- collection_cache . write ( cache_key , rendered_partial . body )
91
- end
91
+ order_by . each_with_object ( { } ) do |cache_key , hash |
92
+ hash [ cache_key ] =
93
+ if content = cached_partials [ cache_key ]
94
+ build_rendered_template ( content , template )
95
+ else
96
+ yield . tap do |rendered_partial |
97
+ collection_cache . write ( cache_key , rendered_partial . body )
98
+ end
99
+ end
92
100
end
93
- end
94
101
end
95
102
end
96
103
end
0 commit comments