From 7d25a7fcda0ed3b9b7a2f6d40e3f56f1999fe37e Mon Sep 17 00:00:00 2001 From: Nick Schwaderer Date: Wed, 15 Nov 2023 12:38:15 +0000 Subject: [PATCH] Fix key in cache instrumentation for Rails 7.2 Previously, the key being logged in cache instrumentation would always be normalized by the instrumenter. However, many cache methods did not pass all of the options necessary to normalize a key into the instrumenter and so the key would not always reflect the actual key used in the cache. In rails/rails@9af99bfffc86700c295772bfd45aaff45e248274, the instrumenter was updated to no longer normalize the key because of this inconsistency. Now the caller of the instrument method is expected to pass the normalized key instead of the pre-normalized key. To maintain compatibility with Rails both before and after this commit, The key passed into instrument is conditional based on the Rails version, and the conditional can eventually be removed once Rails 7.2 is the minimum supported version Co-authored-by: Nick Schwaderer --- lib/active_support/cache/memcached_store.rb | 32 ++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/active_support/cache/memcached_store.rb b/lib/active_support/cache/memcached_store.rb index 92fde8b..7202d5e 100644 --- a/lib/active_support/cache/memcached_store.rb +++ b/lib/active_support/cache/memcached_store.rb @@ -90,11 +90,11 @@ def initialize(*addresses, **options) def append(name, value, options = nil) return true if read_only options = merged_options(options) - normalized_key = normalize_key(name, options) + key = normalize_key(name, options) handle_exceptions(return_value_on_error: nil, on_miss: false, miss_exceptions: [Memcached::NotStored]) do - instrument(:append, name) do - @connection.append(normalized_key, value) + instrument(:append, name_or_key(name, key)) do + @connection.append(key, value) end true end @@ -119,7 +119,7 @@ def read_multi(*names) values = {} handle_exceptions(return_value_on_error: {}) do - instrument(:read_multi, names, options) do + instrument(:read_multi, name_or_key(names, keys_to_names), options) do if raw_values = @connection.get(keys_to_names.keys) raw_values.each do |key, value| entry = deserialize_entry(value) @@ -137,7 +137,7 @@ def cas(name, options = nil) payload = nil success = handle_exceptions(return_value_on_error: false) do - instrument(:cas, name, options) do + instrument(:cas, name_or_key(name, key), options) do @connection.cas(key, expiration(options)) do |raw_value| entry = deserialize_entry(raw_value) value = yield entry.value @@ -166,7 +166,7 @@ def cas_multi(*names, **options) sent_payloads = nil handle_exceptions(return_value_on_error: false) do - instrument(:cas_multi, names, options) do + instrument(:cas_multi, name_or_key(names, keys_to_names), options) do written_payloads = @connection.cas(keys_to_names.keys, expiration(options)) do |raw_values| values = {} @@ -203,18 +203,20 @@ def cas_multi(*names, **options) def increment(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) + key = normalize_key(name, options) handle_exceptions(return_value_on_error: nil) do - instrument(:increment, name, amount: amount) do - @connection.incr(normalize_key(name, options), amount) + instrument(:increment, name_or_key(name, key), amount: amount) do + @connection.incr(key, amount) end end end def decrement(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) + key = normalize_key(name, options) handle_exceptions(return_value_on_error: nil) do - instrument(:decrement, name, amount: amount) do - @connection.decr(normalize_key(name, options), amount) + instrument(:decrement, name_or_key(name, key), amount: amount) do + @connection.decr(key, amount) end end end @@ -344,6 +346,16 @@ def normalize_key(key, options) key end + if ActiveSupport.gem_version >= Gem::Version.new("7.2.0.alpha") + def name_or_key(name, key) + key + end + else + def name_or_key(name, key) + name + end + end + def deserialize_entry(value) unless value.nil? value.is_a?(Entry) ? value : Entry.new(value, compress: false)