From 37d50e8e4d4eec27e0615cda0c6866c27d2b77dc Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Sat, 28 Nov 2009 18:02:30 +0000 Subject: [PATCH 1/2] Rails.cache.fetch should respect false as a present value. --- activesupport/lib/active_support/cache.rb | 10 +++++++--- activesupport/test/caching_test.rb | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index f2d957f..9f4f639 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -168,12 +168,16 @@ module ActiveSupport # sleep(6) # cache.fetch("foo") # => nil def fetch(key, options = {}, &block) - if !options[:force] && value = read(key, options) - value - elsif block_given? + value = if !options[:force] + read(key, options) + end + + if value.nil? && block_given? result = instrument(:generate, key, options, &block) write(key, result, options) result + else + value end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f7..a544ce7 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -101,11 +101,26 @@ module CacheStoreBehavior assert_equal nil, @cache.read('foo') end + def test_should_read_and_write_false + @cache.write('foo', false) + assert_equal false, @cache.read('foo') + end + + def test_should_read_and_write_true + @cache.write('foo', true) + assert_equal true, @cache.read('foo') + end + def test_fetch_without_cache_miss @cache.write('foo', 'bar') assert_equal 'bar', @cache.fetch('foo') { 'baz' } end + def test_fetch_false_without_cache_miss + @cache.write('foo', false) + assert_equal false, @cache.fetch('foo') { 'baz' } + end + def test_fetch_with_cache_miss assert_equal 'baz', @cache.fetch('foo') { 'baz' } end -- 1.6.6 From 6cb522c735d8fa9895b39fc0421583fd4e9f38e8 Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Sat, 28 Nov 2009 18:02:30 +0000 Subject: [PATCH 2/2] Rails.cache.fetch should respect false as a present value. --- activesupport/lib/active_support/cache.rb | 10 +++++++--- activesupport/test/caching_test.rb | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 7213b24..222f7f6 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -169,12 +169,16 @@ module ActiveSupport # sleep(6) # cache.fetch("foo") # => nil def fetch(key, options = {}, &block) - if !options[:force] && value = read(key, options) - value - elsif block_given? + value = if !options[:force] + read(key, options) + end + + if value.nil? && block_given? result = instrument(:generate, key, options, &block) write(key, result, options) result + else + value end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f7..a544ce7 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -101,11 +101,26 @@ module CacheStoreBehavior assert_equal nil, @cache.read('foo') end + def test_should_read_and_write_false + @cache.write('foo', false) + assert_equal false, @cache.read('foo') + end + + def test_should_read_and_write_true + @cache.write('foo', true) + assert_equal true, @cache.read('foo') + end + def test_fetch_without_cache_miss @cache.write('foo', 'bar') assert_equal 'bar', @cache.fetch('foo') { 'baz' } end + def test_fetch_false_without_cache_miss + @cache.write('foo', false) + assert_equal false, @cache.fetch('foo') { 'baz' } + end + def test_fetch_with_cache_miss assert_equal 'baz', @cache.fetch('foo') { 'baz' } end -- 1.6.6