aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/cache.rb
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2016-04-18 08:16:50 -0700
committerJeremy Daer <jeremydaer@gmail.com>2016-04-18 08:35:55 -0700
commitb39131e8bdb65a9ee51e49d7fe8f058e702e173b (patch)
tree82525d1e79ca4bc22f99256a5bad10bad21b20fe /activesupport/lib/active_support/cache.rb
parent71657146374595b6b9b04916649922fa4f5f512d (diff)
parenta712acc42543b575a623e1a8880c0e9dd5505cfa (diff)
downloadrails-b39131e8bdb65a9ee51e49d7fe8f058e702e173b.tar.gz
rails-b39131e8bdb65a9ee51e49d7fe8f058e702e173b.tar.bz2
rails-b39131e8bdb65a9ee51e49d7fe8f058e702e173b.zip
Merge pull request #24577 from mechanicles/fix-fetch-cache-miss
Fix forced cache miss for fetch when called without a block.
Diffstat (limited to 'activesupport/lib/active_support/cache.rb')
-rw-r--r--activesupport/lib/active_support/cache.rb13
1 files changed, 11 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 914390eef1..bc114e0785 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -198,10 +198,17 @@ module ActiveSupport
# cache.fetch('city') # => "Duckburgh"
#
# You may also specify additional options via the +options+ argument.
- # Setting <tt>force: true</tt> will force a cache miss:
+ # Setting <tt>force: true</tt> forces a cache "miss," meaning we treat
+ # the cache value as missing even if it's present. Passing a block is
+ # required when `force` is true so this always results in a cache write.
#
# cache.write('today', 'Monday')
- # cache.fetch('today', force: true) # => nil
+ # cache.fetch('today', force: true) { 'Tuesday' } # => 'Tuesday'
+ # cache.fetch('today', force: true) # => ArgumentError
+ #
+ # The `:force` option is useful when you're calling some other method to
+ # ask whether you should force a cache write. Otherwise, it's clearer to
+ # just call `Cache#write`.
#
# Setting <tt>:compress</tt> will store a large cache entry set by the call
# in a compressed format.
@@ -292,6 +299,8 @@ module ActiveSupport
else
save_block_result_to_cache(name, options) { |_name| yield _name }
end
+ elsif options && options[:force]
+ raise ArgumentError, 'Missing block: Calling `Cache#fetch` with `force: true` requires a block.'
else
read(name, options)
end