Skip to content

cache_response

The decorator that wraps a view method or function-based view in a CachedResponseWrapper. See the cache_response guide for an overview and worked examples.

cache_response

ResponseCacheKeyConstructor

Bases: KeyConstructor

Default key constructor for cache_response. Combines every request query parameter with the wrapped view method's URL kwargs. Subclass to add a user_id partition or narrow the captured fields.

CachedResponseWrapper

CachedResponseWrapper(
    func,
    key_constructor,
    invalidates_on,
    cache_if,
    cache_unless,
    ttl,
    set_cache_headers,
)

Bases: CachedWrapper

CachedWrapper for view methods that returns a fresh HttpResponse on hit.

Stores (content_bytes, status_code, headers_dict). On hit, rebuilds an HttpResponse and skips the view body, serializer, and renderer.

extract_view_and_request

extract_view_and_request(args, kwargs)

Locate the view instance and request in the wrapped method's call args.

cache_response

cache_response(
    key_constructor: KeyConstructor
    | dict
    | type[KeyConstructor]
    | None = None,
    ttl: int | None = 3600,
    invalidates_on: list[InvalidationRule] | None = None,
    cache_if: Callable | None = None,
    cache_unless: Callable | None = None,
    set_cache_headers: bool = False,
)

Cache a view method's rendered HTTP output.

Stores (content, status_code, headers). On a hit, returns a fresh HttpResponse rebuilt from the triple and skips the view body, the serializer, and the renderer. Use this for whole-view caching where cache_result would risk pickling Serializer or QuerySet state.

Pairs with KeyConstructor and InvalidationRule the same way as cache_result. Default key_constructor is ResponseCacheKeyConstructor (query params plus the view's URL kwargs).

When set_cache_headers is True, the wrapper attaches the X-Cached-at, X-Cache-reset-at, and X-Cache-status headers to every returned response so clients and monitoring can tell hits from misses without a separate metadata lookup.

Example

class UserMeAPIView(AsyncAPIView): @cache_response(ttl=60, set_cache_headers=True) async def get(self, request): ...

CachedResponseWrapper

CachedResponseWrapper(
    func,
    key_constructor,
    invalidates_on,
    cache_if,
    cache_unless,
    ttl,
    set_cache_headers,
)

Bases: CachedWrapper

CachedWrapper for view methods that returns a fresh HttpResponse on hit.

Stores (content_bytes, status_code, headers_dict). On hit, rebuilds an HttpResponse and skips the view body, serializer, and renderer.

render_view_response_sync

render_view_response_sync(args, kwargs)

render_view_response_async async

render_view_response_async(args, kwargs)

extract_view_and_request

extract_view_and_request(args, kwargs)

Locate the view instance and request in the wrapped method's call args.

serialize_response

serialize_response(response)

rebuild_http_response

rebuild_http_response(triple)

ResponseCacheKeyConstructor

Bases: KeyConstructor

Default key constructor for cache_response. Combines every request query parameter with the wrapped view method's URL kwargs. Subclass to add a user_id partition or narrow the captured fields.