Handle empty list of cache keys

Fix: https://github.com/rails/rails/pull/48145

`read_multi`, `write_multi` and `fetch multi` should all
bail out early if somehow called with an empty list.

Co-Authored-By: Joshua Young <djry1999@gmail.com>
This commit is contained in:
Jean Boussier 2023-05-07 11:29:59 +09:00
parent 490804f7d5
commit d3b5cdf220
4 changed files with 25 additions and 0 deletions

@ -946,6 +946,12 @@ def b.to_partial_path; "test/partial_iteration_2"; end
end
end
test "collection caching with empty collection and logger with level debug" do
ActionView::PartialRenderer.collection_cache.logger = Logger.new(nil, level: :debug)
assert_nil @view.render(partial: "test/cached_customer", collection: [], cached: true)
end
test "collection caching with repeated collection" do
sets = [
[1, 2, 3, 4, 5],

@ -437,6 +437,8 @@ def read(name, options = nil)
#
# Returns a hash mapping the names provided to the values found.
def read_multi(*names)
return {} if names.empty?
options = names.extract_options!
options = merged_options(options)
@ -449,6 +451,8 @@ def read_multi(*names)
# Cache Storage API to write multiple values at once.
def write_multi(hash, options = nil)
return hash if hash.empty?
options = merged_options(options)
instrument :write_multi, hash, options do |payload|
@ -491,6 +495,7 @@ def write_multi(hash, options = nil)
# # => nil
def fetch_multi(*names)
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)

@ -175,6 +175,8 @@ def inspect
# Read multiple values at once. Returns a hash of requested keys ->
# fetched values.
def read_multi(*names)
return {} if names.empty?
options = names.extract_options!
instrument(:read_multi, names, options) do |payload|
read_multi_entries(names, **options).tap do |results|

@ -147,6 +147,10 @@ def test_read_multi
assert_equal({ key => "bar", other_key => "baz" }, @cache.read_multi(key, other_key))
end
def test_read_multi_empty_list
assert_equal({}, @cache.read_multi())
end
def test_read_multi_with_expires
time = Time.now
key = SecureRandom.uuid
@ -159,6 +163,10 @@ def test_read_multi_with_expires
end
end
def test_write_multi_empty_hash
assert @cache.write_multi({})
end
def test_fetch_multi
key = SecureRandom.uuid
other_key = SecureRandom.uuid
@ -172,6 +180,10 @@ def test_fetch_multi
assert_equal((third_key * 2), @cache.read(third_key))
end
def test_fetch_multi_empty_hash
assert_equal({}, @cache.fetch_multi() { raise "Not called" })
end
def test_fetch_multi_without_expires_in
key = SecureRandom.uuid
other_key = SecureRandom.uuid