This project is archived and is in readonly mode.

#1830 ✓stale
Thijs de Vries

Rails.cache.fetch does not work with false boolean as cached value

Reported by Thijs de Vries | January 31st, 2009 @ 10:19 PM | in 3.x

If I run

some_bool_value = Rails.cache.fetch("some_key") {expensive_function_returns_false()}

Rails will miss on the fetch every time. However if I do

if Rails.cache.exist?("some_key")
  some_bool_value ="some_key")
  some_bool_value = Rails.cache.write("some_key", expensive_function_returns_false())

The cache will miss the first time and than hit every time after that. I was looking at the code in rails / activesupport / lib / active_support / cache.rb and noticed on line 141 there is the following if statement:

if !options[:force] && value = read(key, options)

value is set to false in the if statement, causing the condition to fail and miss. It would be better if value was read after the if statement and the if statement would be changed to

if !options[:force] && exist?(key)

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>