From d9e9e46a3985951c59d7f673f7643e4e0a24664b Mon Sep 17 00:00:00 2001 From: Chris Kampmeier Date: Sat, 14 Nov 2009 16:02:36 -0800 Subject: [PATCH] Don't interpret the cached value 'false' as a cache miss --- activesupport/lib/active_support/cache.rb | 2 +- activesupport/test/caching_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index f2d957f..04ff1b5 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -168,7 +168,7 @@ module ActiveSupport # sleep(6) # cache.fetch("foo") # => nil def fetch(key, options = {}, &block) - if !options[:force] && value = read(key, options) + if !options[:force] && (value = read(key, options)) != nil value elsif block_given? result = instrument(:generate, key, options, &block) diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f7..1aa1e2b 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -60,6 +60,12 @@ class CacheStoreTest < ActiveSupport::TestCase @cache = ActiveSupport::Cache.lookup_store(:memory_store) end + def test_fetch_of_boolean_false_without_cache_miss + @cache.stubs(:read).with('foo', {}).returns(false) + @cache.expects(:write).never + assert_equal false, @cache.fetch('foo') { 'unused' } + end + def test_fetch_without_cache_miss @cache.stubs(:read).with('foo', {}).returns('bar') @cache.expects(:write).never -- 1.6.4.1