diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index af89c7df24d81..79c0c19b39909 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -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 diff --git a/activesupport/test/cache/behaviors/cache_store_behavior.rb b/activesupport/test/cache/behaviors/cache_store_behavior.rb index 46b141a640166..57e73dba2c6c8 100644 --- a/activesupport/test/cache/behaviors/cache_store_behavior.rb +++ b/activesupport/test/cache/behaviors/cache_store_behavior.rb @@ -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