summaryrefslogtreecommitdiff
path: root/django/middleware
diff options
context:
space:
mode:
authorRinat Khabibiev <srenskiy@gmail.com>2016-09-15 08:41:07 +0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-02-05 13:27:51 +0100
commit3580b47ed31ec85ae89b13618f36bb463e97acc8 (patch)
tree6835fcee24746b2748aa8563e684180ae34a7afb /django/middleware
parent4b1cd8edc10bb6c3da3a270180d028670a6f2110 (diff)
Fixed #27225 -- Added "Age" header when fetching cached responses.
Co-Authored-By: Author: Alexander Lazarević <laza@e11bits.com>
Diffstat (limited to 'django/middleware')
-rw-r--r--django/middleware/cache.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/django/middleware/cache.py b/django/middleware/cache.py
index 0fdffe1bbe..196b1995ff 100644
--- a/django/middleware/cache.py
+++ b/django/middleware/cache.py
@@ -43,6 +43,8 @@ More details about how the caching works:
"""
+import time
+
from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS, caches
from django.utils.cache import (
@@ -53,6 +55,7 @@ from django.utils.cache import (
patch_response_headers,
)
from django.utils.deprecation import MiddlewareMixin
+from django.utils.http import parse_http_date_safe
class UpdateCacheMiddleware(MiddlewareMixin):
@@ -171,6 +174,15 @@ class FetchFromCacheMiddleware(MiddlewareMixin):
request._cache_update_cache = True
return None # No cache information available, need to rebuild.
+ # Derive the age estimation of the cached response.
+ if (max_age_seconds := get_max_age(response)) is not None and (
+ expires_timestamp := parse_http_date_safe(response["Expires"])
+ ) is not None:
+ now_timestamp = int(time.time())
+ remaining_seconds = expires_timestamp - now_timestamp
+ # Use Age: 0 if local clock got turned back.
+ response["Age"] = max(0, max_age_seconds - remaining_seconds)
+
# hit, return cached response
request._cache_update_cache = False
return response