Fixed #33252 -- Made cache middlewares thread-safe.

This commit is contained in:
Iuri de Silvio 2021-10-31 21:54:13 -03:00 committed by Mariusz Felisiak
parent 0c05c183e4
commit 3ff7b15bb7
2 changed files with 23 additions and 3 deletions

15
tests/cache/tests.py vendored
View file

@ -2488,6 +2488,21 @@ class CacheMiddlewareTest(SimpleTestCase):
self.assertIn('Cache-Control', response)
self.assertIn('Expires', response)
def test_per_thread(self):
"""The cache instance is different for each thread."""
thread_caches = []
middleware = CacheMiddleware(empty_response)
def runner():
thread_caches.append(middleware.cache)
for _ in range(2):
thread = threading.Thread(target=runner)
thread.start()
thread.join()
self.assertIsNot(thread_caches[0], thread_caches[1])
@override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',