Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion django/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ def _i18n_cache_key_suffix(request, cache_key):


def _generate_cache_key(request, method, headerlist, key_prefix):
"""Return a cache key from the headers given in the header list."""
"""
Return a cache key from the headers given in the header list.
"""
ctx = md5(usedforsecurity=False)
for header in headerlist:
value = request.META.get(header)
Expand Down
25 changes: 25 additions & 0 deletions tests/cache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,8 @@ def hello_world_view(request, value):
def csrf_view(request):
return HttpResponse(csrf(request)["csrf_token"])

def invalidate_cache_from_uri(uri):
pass

@override_settings(
CACHE_MIDDLEWARE_ALIAS="other",
Expand Down Expand Up @@ -2578,6 +2580,29 @@ def test_middleware(self):
self.assertIsNotNone(result)
self.assertEqual(result.content, b"Hello World 1")


def test_view_cache_invalidation(self):
default_view = cache_page(3)(hello_world_view)
request = self.factory.get("/view/")

# Request the view once
response = default_view(request, "1")
self.assertEqual(response.content, b"Hello World 1")

# Request again -- hit the cache
response = default_view(request, "2")
self.assertEqual(response.content, b"Hello World 1")

# invalidate the cache for the view
uri = request.build_absolute_uri().encode("ascii")
print(uri)
invalidate_cache_from_uri(uri)


# Request again -- do not hit the cache
response = default_view(request, "3")
self.assertEqual(response.content, b"Hello World 3")

def test_view_decorator(self):
# decorate the same view with different cache decorators
default_view = cache_page(3)(hello_world_view)
Expand Down