mirror of
https://github.com/django/django.git
synced 2025-04-04 05:26:43 +00:00
Fixed #25782 – duplicate cache skip on header
This commit is contained in:
parent
f04d3d4c0d
commit
b4f0ca776c
@ -121,11 +121,10 @@ class UpdateCacheMiddleware(MiddlewareMixin):
|
||||
cache_key = learn_cache_key(
|
||||
request, response, timeout, self.key_prefix, cache=self.cache
|
||||
)
|
||||
# check header for "x-successive-cache" to avoid multiple cache sets
|
||||
# if not found, set it as key used
|
||||
if response.has_header("x-successive-cache"):
|
||||
# avoid multiple cache sets
|
||||
if response.has_header("X-Cache-Success"):
|
||||
return response
|
||||
response["x-successive-cache"] = cache_key
|
||||
response["X-Cache-Success"] = "true"
|
||||
|
||||
if hasattr(response, "render") and callable(response.render):
|
||||
response.add_post_render_callback(
|
||||
|
22
tests/cache/tests.py
vendored
22
tests/cache/tests.py
vendored
@ -2996,11 +2996,13 @@ class CacheHandlerTest(SimpleTestCase):
|
||||
},
|
||||
)
|
||||
class DuplicateCacheKey(TestCase):
|
||||
def test_cache_page_conflict(self):
|
||||
@mock.patch("time.sleep")
|
||||
def test_cache_page_conflict(self, mock_sleep):
|
||||
"""cache_page isn't causing duplicates with middlewares"""
|
||||
|
||||
@cache_page(3000, cache="other")
|
||||
def test_view(request):
|
||||
time.sleep(1)
|
||||
return HttpResponse(b"Hello, world")
|
||||
|
||||
request = RequestFactory().get("/view/")
|
||||
@ -3009,16 +3011,22 @@ class DuplicateCacheKey(TestCase):
|
||||
"5b30bdb36b35025daf3570b18c3bb916.d41d8cd98f00b204e9800998ecf8427e.en"
|
||||
)
|
||||
|
||||
self.assertEqual(caches["default"].has_key(cache_key), False)
|
||||
self.assertEqual(caches["other"].has_key(cache_key), False)
|
||||
self.assertFalse(caches["default"].has_key(cache_key))
|
||||
self.assertFalse(caches["other"].has_key(cache_key))
|
||||
|
||||
UpdateCacheMiddleware(test_view)(request)
|
||||
self.assertEqual(caches["other"].has_key(cache_key), True)
|
||||
self.assertEqual(caches["default"].has_key(cache_key), False)
|
||||
self.assertTrue(caches["other"].has_key(cache_key))
|
||||
self.assertFalse(caches["default"].has_key(cache_key))
|
||||
mock_sleep.assert_called_once()
|
||||
mock_sleep.reset_mock()
|
||||
|
||||
CacheMiddleware(test_view)(request)
|
||||
self.assertEqual(caches["other"].has_key(cache_key), True)
|
||||
self.assertEqual(caches["default"].has_key(cache_key), False)
|
||||
self.assertTrue(caches["other"].has_key(cache_key))
|
||||
self.assertFalse(caches["default"].has_key(cache_key))
|
||||
mock_sleep.assert_not_called()
|
||||
|
||||
response = test_view(request)
|
||||
self.assertEqual(response.content, b"Hello, world")
|
||||
self.assertTrue(caches["other"].has_key(cache_key))
|
||||
self.assertFalse(caches["default"].has_key(cache_key))
|
||||
mock_sleep.assert_not_called()
|
||||
|
Loading…
x
Reference in New Issue
Block a user