lib/active_support/cache.rb in activesupport-7.2.2 vs lib/active_support/cache.rb in activesupport-8.0.0.beta1
- old
+ new
@@ -536,25 +536,27 @@
def read_multi(*names)
return {} if names.empty?
options = names.extract_options!
options = merged_options(options)
+ keys = names.map { |name| normalize_key(name, options) }
- instrument_multi :read_multi, names, options do |payload|
+ instrument_multi :read_multi, keys, options do |payload|
read_multi_entries(names, **options, event: payload).tap do |results|
- payload[:hits] = results.keys
+ payload[:hits] = results.keys.map { |name| normalize_key(name, options) }
end
end
end
# Cache Storage API to write multiple values at once.
def write_multi(hash, options = nil)
return hash if hash.empty?
options = merged_options(options)
+ normalized_hash = hash.transform_keys { |key| normalize_key(key, options) }
- instrument_multi :write_multi, hash, options do |payload|
+ instrument_multi :write_multi, normalized_hash, options do |payload|
entries = hash.each_with_object({}) do |(name, value), memo|
memo[normalize_key(name, options)] = Entry.new(value, **options.merge(version: normalize_version(name, options)))
end
write_multi_entries entries, **options
@@ -594,13 +596,13 @@
raise ArgumentError, "Missing block: `Cache#fetch_multi` requires a block." unless block_given?
return {} if names.empty?
options = names.extract_options!
options = merged_options(options)
-
+ keys = names.map { |name| normalize_key(name, options) }
writes = {}
- ordered = instrument_multi :read_multi, names, options do |payload|
+ ordered = instrument_multi :read_multi, keys, options do |payload|
if options[:force]
reads = {}
else
reads = read_multi_entries(names, **options)
end
@@ -608,11 +610,11 @@
ordered = names.index_with do |name|
reads.fetch(name) { writes[name] = yield(name) }
end
writes.compact! if options[:skip_nil]
- payload[:hits] = reads.keys
+ payload[:hits] = reads.keys.map { |name| normalize_key(name, options) }
payload[:super_operation] = :fetch_multi
ordered
end
@@ -941,12 +943,15 @@
#
# With a namespace block:
#
# namespace_key 'foo', namespace: -> { 'cache' }
# # => 'cache:foo'
- def namespace_key(key, options = nil)
- options = merged_options(options)
- namespace = options[:namespace]
+ def namespace_key(key, call_options = nil)
+ namespace = if call_options&.key?(:namespace)
+ call_options[:namespace]
+ else
+ options[:namespace]
+ end
if namespace.respond_to?(:call)
namespace = namespace.call
end