From 456b0bfb56e66e450dc2fdc7c45a8b4a2209fe13 Mon Sep 17 00:00:00 2001 From: Brian Moran Date: Wed, 11 Nov 2009 15:54:09 -0800 Subject: [PATCH] check for existence vs. value being non-nil --- activesupport/lib/active_support/cache.rb | 4 ++-- activesupport/test/caching_test.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index f2d957f..c4f6ff1 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -168,8 +168,8 @@ module ActiveSupport # sleep(6) # cache.fetch("foo") # => nil def fetch(key, options = {}, &block) - if !options[:force] && value = read(key, options) - value + if !options[:force] && exist?(key, options) + read(key, options) elsif block_given? result = instrument(:generate, key, options, &block) write(key, result, options) diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 6a51ce9..bbf35b3 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -58,7 +58,15 @@ class CacheStoreTest < ActiveSupport::TestCase @cache = ActiveSupport::Cache.lookup_store(:memory_store) end - def test_fetch_without_cache_miss + def test_fetch_of_boolean_false_without_cache_miss + @cache.stubs(:exist?).with('foo_boolean_false', {}).returns(true) + @cache.stubs(:read).with('foo_boolean_false', {}).returns(false) + @cache.expects(:write).never + assert_equal false, @cache.fetch('foo_boolean_false') { 'blech' } + end + + def test_fetch_without_cache_miss + @cache.stubs(:exist?).with('foo', {}).returns(true) @cache.stubs(:read).with('foo', {}).returns('bar') @cache.expects(:write).never assert_equal 'bar', @cache.fetch('foo') { 'baz' } -- 1.6.1.2