From c046e60965d6a9af9083579bcc7d52ca650d885f Mon Sep 17 00:00:00 2001 From: Parker Selbert Date: Wed, 17 Jul 2013 13:59:15 -0500 Subject: 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. --- activesupport/lib/active_support/cache.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3