Skip to content

Docs: dependencies still run on a cache hit; BackgroundTasks run only on a miss #329

Description

@allen0099

Problem

CACHE_FLOW.md says that on a cache hit "the endpoint handler does not run at all". That holds for the function body, but:

  • FastAPI dependencies are resolved before the @cache wrapper, so they run on every request, hit or miss (expensive_dep ran 4 times for 4 requests). Per-user key builders rely on this, since they read request.state set by a dependency.
  • BackgroundTasks added in the handler run only on a miss.

People put DB sessions, audit logging or rate limiting in dependencies and background tasks, so both points matter.

Proposal

Add a short "What still runs on a hit" note to CACHE_FLOW.md and HTTP_CACHING.md (and zh-TW).

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

    documentationImprovements or additions to documentationhttp-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