From 608ab043f75f1f9c094de57d2fd678f522bb8243 Mon Sep 17 00:00:00 2001
From: Christos Kopanos <ckopanos@redmob.gr>
Date: Tue, 5 Jul 2022 19:23:05 +0300
Subject: [PATCH] Fixed #33826 -- Fixed RedisCache.set_many()/delete_many()
 crash with an empty list.

---
 django/core/cache/backends/redis.py | 4 ++++
 tests/cache/tests.py                | 6 ++++++
 2 files changed, 10 insertions(+)

diff --git a/django/core/cache/backends/redis.py b/django/core/cache/backends/redis.py
index cb55be71c2..c370ff5cb7 100644
--- a/django/core/cache/backends/redis.py
+++ b/django/core/cache/backends/redis.py
@@ -214,6 +214,8 @@ class RedisCache(BaseCache):
         return self._cache.incr(key, delta)
 
     def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None):
+        if not data:
+            return []
         safe_data = {}
         for key, value in data.items():
             key = self.make_and_validate_key(key, version=version)
@@ -222,6 +224,8 @@ class RedisCache(BaseCache):
         return []
 
     def delete_many(self, keys, version=None):
+        if not keys:
+            return
         safe_keys = [self.make_and_validate_key(key, version=version) for key in keys]
         self._cache.delete_many(safe_keys)
 
diff --git a/tests/cache/tests.py b/tests/cache/tests.py
index 4a102d0cc2..937a55acc5 100644
--- a/tests/cache/tests.py
+++ b/tests/cache/tests.py
@@ -577,6 +577,9 @@ class BaseCacheTests:
         self.assertIsNone(cache.get("key1"))
         self.assertIsNone(cache.get("key2"))
 
+    def test_set_many_empty_data(self):
+        self.assertEqual(cache.set_many({}), [])
+
     def test_delete_many(self):
         # Multiple keys can be deleted using delete_many
         cache.set_many({"key1": "spam", "key2": "eggs", "key3": "ham"})
@@ -585,6 +588,9 @@ class BaseCacheTests:
         self.assertIsNone(cache.get("key2"))
         self.assertEqual(cache.get("key3"), "ham")
 
+    def test_delete_many_no_keys(self):
+        self.assertIsNone(cache.delete_many([]))
+
     def test_clear(self):
         # The cache can be emptied using clear
         cache.set_many({"key1": "spam", "key2": "eggs"})