Skip to content

Commit

Permalink
[Fix #49796] Prevent global cache options being overwritten
Browse files Browse the repository at this point in the history
The reason of removing dup 5 years ago in 6da99b4 was to decrease memory allocations, so, it makes sense to only dup options when we know they will be overwritten.
  • Loading branch information
HolyWalley authored and jonathanhefner committed Oct 26, 2023
1 parent 35aa5e3 commit f455d5b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/cache.rb
Expand Up @@ -1053,6 +1053,8 @@ def get_entry_value(entry, name, options)
end

def save_block_result_to_cache(name, options)
options = options.dup

result = instrument(:generate, name, options) do
yield(name, WriteOptions.new(options))
end
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/cache/behaviors/cache_store_behavior.rb
Expand Up @@ -702,6 +702,17 @@ def test_cache_miss_instrumentation
ActiveSupport::Notifications.unsubscribe "cache_read.active_support"
end

def test_setting_options_in_fetch_block_does_not_change_cache_options
key = SecureRandom.uuid

assert_no_changes -> { @cache.options.dup } do
@cache.fetch(key) do |_key, options|
options.expires_in = 5.minutes
"bar"
end
end
end

private
def with_raise_on_invalid_cache_expiration_time(new_value, &block)
old_value = ActiveSupport::Cache::Store.raise_on_invalid_cache_expiration_time
Expand Down

0 comments on commit f455d5b

Please sign in to comment.