From f88ccf8d9ebff9250bda757f0b06120673d0d318 Mon Sep 17 00:00:00 2001 From: Matthew Higgins Date: Fri, 11 Sep 2009 00:48:24 -0700 Subject: [PATCH] Add read_multi to all cache stores --- activesupport/lib/active_support/cache.rb | 9 +++++++++ activesupport/test/caching_test.rb | 17 ++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index e28df8e..3738048 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -244,6 +244,15 @@ module ActiveSupport end end + def read_multi(*keys) + keys.flatten.inject({}) do |results, key| + if (value = read(key)) + results[key] = value + end + results + end + end + private def expires_in(options) expires_in = options && options[:expires_in] diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 7667f11..edd5a29 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -135,6 +135,14 @@ module CacheStoreBehavior assert @cache.exist?('foo') assert !@cache.exist?('bar') end + + def test_read_multi + @cache.write('foo', 1) + @cache.write('goo', 2) + assert_equal({'foo' => 1, 'goo' => 2}, @cache.read_multi('foo', 'goo')) + assert_equal({'foo' => 1, 'goo' => 2}, @cache.read_multi(['foo', 'goo'])) + assert_equal({}, @cache.read_multi('faz')) + end end class FileStoreTest < ActiveSupport::TestCase @@ -297,15 +305,6 @@ uses_memcached 'memcached backed store' do end end - def test_multi_get - @cache.with_local_cache do - @cache.write('foo', 1) - @cache.write('goo', 2) - result = @cache.read_multi('foo', 'goo') - assert_equal({'foo' => 1, 'goo' => 2}, result) - end - end - def test_middleware app = lambda { |env| result = @cache.write('foo', 'bar') -- 1.6.4.2