Skip to content

Commit

Permalink
Use name or key based on Rails/ActiveSupport >= 7.2
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Schwaderer <[email protected]>
  • Loading branch information
Schwad committed Nov 15, 2023
1 parent 2af3d60 commit fc8d293
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/active_support/cache/memcached_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ def initialize(*addresses, **options)
end

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, normalized_key) do
@connection.append(normalized_key, value)
instrument(:append, name_or_key(name, key)) do
@connection.append(key, value)
end
true
end
Expand All @@ -120,7 +119,7 @@ def read_multi(*names)
values = {}

handle_exceptions(return_value_on_error: {}) do
instrument(:read_multi, keys_to_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)
Expand All @@ -138,7 +137,7 @@ def cas(name, options = nil)
payload = nil

success = handle_exceptions(return_value_on_error: false) do
instrument(:cas, key, 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
Expand Down Expand Up @@ -167,7 +166,7 @@ def cas_multi(*names, **options)
sent_payloads = nil

handle_exceptions(return_value_on_error: false) do
instrument(:cas_multi, keys_to_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 = {}

Expand Down Expand Up @@ -206,7 +205,7 @@ 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, key, amount: amount) do
instrument(:increment, name_or_key(name, key), amount: amount) do
@connection.incr(key, amount)
end
end
Expand All @@ -216,7 +215,7 @@ 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, key, amount: amount) do
instrument(:decrement, name_or_key(name, key), amount: amount) do
@connection.decr(key, amount)
end
end
Expand Down Expand Up @@ -346,6 +345,15 @@ def normalize_key(key, options)
key
end

def name_or_key(name, key)
# Rails may not always be defined, ActiveSupport will be.
if ActiveSupport::VERSION::STRING >= "7.2"
key
else
name
end
end

def deserialize_entry(value)
unless value.nil?
value.is_a?(Entry) ? value : Entry.new(value, compress: false)
Expand Down

0 comments on commit fc8d293

Please sign in to comment.