aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorParker Selbert <parker@sorentwo.com>2013-07-17 13:59:15 -0500
committerParker Selbert <parker@sorentwo.com>2014-02-26 20:31:46 -0600
commitc046e60965d6a9af9083579bcc7d52ca650d885f (patch)
tree8e5f3b75bd66cc786449f2418b5aa5e880fe4a9f /activesupport/lib/active_support
parent7c92e0bbf1848d0c8bc191b04cbe9272a82e3782 (diff)
downloadrails-c046e60965d6a9af9083579bcc7d52ca650d885f.tar.gz
rails-c046e60965d6a9af9083579bcc7d52ca650d885f.tar.bz2
rails-c046e60965d6a9af9083579bcc7d52ca650d885f.zip
Return a hash rather than array from fetch_multi
The current implementation of `fetch_multi` returns an array and has no means to easily backtrack which names yielded which results. By changing the return value to a Hash we retain the name information. Hash#values can be used on the response if only the values are needed.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/cache.rb11
1 files changed, 5 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 2b7f5943b5..a627fa8651 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -357,20 +357,19 @@ module ActiveSupport
#
# Options are passed to the underlying cache implementation.
#
- # Returns an array with the data for each of the names. For example:
+ # Returns a hash with the data for each of the names. For example:
#
# cache.write("bim", "bam")
- # cache.fetch_multi("bim", "boom") {|key| key * 2 }
- # # => ["bam", "boomboom"]
+ # cache.fetch_multi("bim", "boom") { |key| key * 2 }
+ # # => { "bam" => "bam", "boom" => "boomboom" }
#
def fetch_multi(*names)
options = names.extract_options!
options = merged_options(options)
-
results = read_multi(*names, options)
- names.map do |name|
- results.fetch(name) do
+ names.each_with_object({}) do |name, memo|
+ memo[name] = results.fetch(name) do
value = yield name
write(name, value, options)
value