diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-10-25 09:07:16 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-10-25 09:07:16 -0700 |
commit | f5ae64dc284712089adb66b55c2a9abcc3f6d8b4 (patch) | |
tree | 1e05b5a35e3a4182f7ceb3b32255e58a5e488d21 | |
parent | 15821005c16bafbcbec92cdf0d535c027d5d3718 (diff) | |
parent | 96f5ceaafb857722be16efcff7ece1a20baa76f0 (diff) | |
download | rails-f5ae64dc284712089adb66b55c2a9abcc3f6d8b4.tar.gz rails-f5ae64dc284712089adb66b55c2a9abcc3f6d8b4.tar.bz2 rails-f5ae64dc284712089adb66b55c2a9abcc3f6d8b4.zip |
Merge pull request #8013 from noahhendrix/master
Pass key to block in cache.fetch on miss
-rw-r--r-- | activesupport/lib/active_support/cache.rb | 10 | ||||
-rw-r--r-- | activesupport/test/caching_test.rb | 14 |
2 files changed, 17 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 732ab4b7df..9a53870b3d 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -182,10 +182,10 @@ module ActiveSupport # the cache with the given key, then that data is returned. # # If there is no such data in the cache (a cache miss), then +nil+ will be - # returned. However, if a block has been passed, that block will be run - # in the event of a cache miss. The return value of the block will be - # written to the cache under the given cache key, and that return value - # will be returned. + # returned. However, if a block has been passed, that block will be passed + # the key and executed in the event of a cache miss. The return value of the + # block will be written to the cache under the given cache key, and that + # return value will be returned. # # cache.write('today', 'Monday') # cache.fetch('today') # => "Monday" @@ -300,7 +300,7 @@ module ActiveSupport entry.value else result = instrument(:generate, name, options) do |payload| - yield + yield(name) end write(name, result, options) result diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 9f76f4c90b..ed903746c8 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -83,7 +83,7 @@ class CacheKeyTest < ActiveSupport::TestCase def test_expand_cache_key_of_true assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true) end - + def test_expand_cache_key_of_array_like_object assert_equal 'foo/bar/baz', ActiveSupport::Cache.expand_cache_key(%w{foo bar baz}.to_enum) end @@ -197,6 +197,16 @@ module CacheStoreBehavior assert_equal 'baz', @cache.fetch('foo') { 'baz' } end + def test_fetch_with_cache_miss_passes_key_to_block + cache_miss = false + assert_equal 3, @cache.fetch('foo') { |key| cache_miss = true; key.length } + assert cache_miss + + cache_miss = false + assert_equal 3, @cache.fetch('foo') { |key| cache_miss = true; key.length } + assert !cache_miss + end + def test_fetch_with_forced_cache_miss @cache.write('foo', 'bar') @cache.expects(:read).never @@ -594,7 +604,7 @@ class FileStoreTest < ActiveSupport::TestCase assert_equal "views/index?id=1", @cache_with_pathname.send(:file_path_key, key) end - # Test that generated cache keys are short enough to have Tempfile stuff added to them and + # Test that generated cache keys are short enough to have Tempfile stuff added to them and # remain valid def test_filename_max_size key = "#{'A' * ActiveSupport::Cache::FileStore::FILENAME_MAX_SIZE}" |