diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-06 18:52:57 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-06 18:52:57 -0700 |
commit | 33283c98ed690b12bc2bca75276236dc907798b2 (patch) | |
tree | 8c3106531a93af1114e209bb94b5d8a2cbcef4f4 /activesupport/lib/active_support | |
parent | 925728c283f2d6f8f41bf79f6958494f14560aeb (diff) | |
parent | 36d41a15c35e6f4b698931987b2115e221d0fcfa (diff) | |
download | rails-33283c98ed690b12bc2bca75276236dc907798b2.tar.gz rails-33283c98ed690b12bc2bca75276236dc907798b2.tar.bz2 rails-33283c98ed690b12bc2bca75276236dc907798b2.zip |
Merge pull request #10234 from dasch/dasch/fetch-multi
Allow fetching multiple values from the cache at once
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/cache.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 6c220ae625..b1ab5570a8 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -352,6 +352,34 @@ module ActiveSupport results end + # Fetches data from the cache, using the given keys. If there is data in + # the cache with the given keys, then that data is returned. Otherwise, + # the supplied block is called for each key for which there was no data, + # and the result will be written to the cache and returned. + # + # Options are passed to the underlying cache implementation. + # + # Returns an array with the data for each of the names. For example: + # + # cache.write("bim", "bam") + # cache.fetch_multi("bim", "boom") {|key| key * 2 } + # #=> ["bam", "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 + value = yield name + write(name, value, options) + value + end + end + end + # Writes the value to the cache, with the key. # # Options are passed to the underlying cache implementation. |