Source code for ferrosoft.apps.ferrobase.middleware.cache

#  Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.conf import settings
from django.http import HttpResponse


[docs] class CacheControlMiddleware: """Middleware that ensures every response carries a ``Cache-Control`` header. If the upstream view already set the header, it is left untouched. Otherwise the value is taken from ``settings.CACHE_CONTROL``, falling back to ``"no-store"`` so sensitive platform responses are never cached by default. """ async_capable = True sync_capable = False _HEADER = "Cache-Control" _CONFIG = "CACHE_CONTROL" _DEFAULT = "no-store" def __init__(self, get_response): self.get_response = get_response if iscoroutinefunction(self.get_response): markcoroutinefunction(self) async def __call__(self, request): response: HttpResponse = await self.get_response(request) if self._HEADER not in response.headers: response.headers["Cache-Control"] = getattr( settings, self._CONFIG, self._DEFAULT ) return response