Skip to content

Invalidate a cached route without hand-building a Request #332

Description

@allen0099

Problem

Invalidating a cached GET route from the POST/PUT handler that changes the data needs a hand-built request. The documented recipe copies the ASGI scope and rewrites method, path and query_string:

scope = dict(request.scope)
scope["method"] = "GET"
scope["path"] = f"/items/{item_id}"
scope["query_string"] = b""
await invalidate(StarletteRequest(scope))

It is the most awkward snippet in the docs. The obvious call, invalidate(request) with the PUT request itself, quietly returns False, because it builds the PUT key.

Proposal

Add a helper that builds the key without a hand-made request. Two options:

  • await invalidate_path("/items/1", request=request, query=...), which takes host and scheme from the current request and uses GET;
  • a method on the decorated route, e.g. await read_item.invalidate(item_id=1).

Either way it must honour key_builder and vary, as invalidate() does. It should also say clearly (warn or document) that invalidate() uses the method of the request it is given.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    developer-experienceSurprising behaviour, missing warnings or unclear errors for library usersenhancementNew feature or requesthttp-cacheThe @cache decorator, cache keys and Cache-Control handling

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions