From ca798c0a1e386186710de147f3f7dba5473079ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fran=C3=A7a?= Date: Tue, 9 Feb 2021 16:08:33 -0500 Subject: [PATCH] Merge pull request #41381 from movermeyer/allow_for_nil_addresses_from_dalli_store Allow addresses to support `nil` values --- activesupport/CHANGELOG.md | 21 +++++++++++++++++++ .../active_support/cache/mem_cache_store.rb | 2 +- .../test/cache/stores/mem_cache_store_test.rb | 8 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index e6d7d76acc305..8ebfb4e393012 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,24 @@ +* `ActiveSupport::Cache::MemCacheStore` now accepts an explicit `nil` for its `addresses` argument. + + ```ruby + config.cache_store = :mem_cache_store, nil + + # is now equivalent to + + config.cache_store = :mem_cache_store + + # and is also equivalent to + + config.cache_store = :mem_cache_store, ENV["MEMCACHE_SERVERS"] || "localhost:11211" + + # which is the fallback behavior of Dalli + ``` + + This helps those migrating from `:dalli_store`, where an explicit `nil` was permitted. + + *Michael Overmeyer* + + ## Rails 6.1.1 (January 07, 2021) ## * Change `IPAddr#to_json` to match the behavior of the json gem returning the string representation diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index bb6ce88206096..d783b92819183 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -64,7 +64,7 @@ def self.supports_cache_versioning? def self.build_mem_cache(*addresses) # :nodoc: addresses = addresses.flatten options = addresses.extract_options! - addresses = nil if addresses.empty? + addresses = nil if addresses.compact.empty? pool_options = retrieve_pool_options(options) if pool_options.empty? diff --git a/activesupport/test/cache/stores/mem_cache_store_test.rb b/activesupport/test/cache/stores/mem_cache_store_test.rb index b96347944f7cc..c8967f94bd425 100644 --- a/activesupport/test/cache/stores/mem_cache_store_test.rb +++ b/activesupport/test/cache/stores/mem_cache_store_test.rb @@ -187,6 +187,14 @@ def test_falls_back_to_localhost_if_no_address_provided_and_memcache_servers_und end end + def test_falls_back_to_localhost_if_address_provided_as_nil + with_memcache_servers_environment_variable(nil) do + cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, nil) + + assert_equal ["127.0.0.1:11211"], servers(cache) + end + end + def test_falls_back_to_localhost_if_no_address_provided_and_memcache_servers_defined with_memcache_servers_environment_variable("custom_host") do cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)