Skip to content

Commit

Permalink
Rails.cache.fetch should respect false as a present value.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewrudy committed Mar 2, 2010
1 parent 11db694 commit 6cb522c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,16 @@ def self.instrument
# 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

Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,26 @@ def test_should_read_and_write_nil
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
Expand Down

0 comments on commit 6cb522c

Please sign in to comment.