Load a cached value using stale-while-revalidate semantics.
This helper is server-side only and is intended for caching internal async dependencies such as auth or metadata lookups.
swr(
key: str,
loader: Callable[[], Awaitable[T]],
*,
fresh_for: timedelta | None = None,
max_age: timedelta | None = None,
model: type[T] | None = None
) -> SWRResult[T]Semantics:
loader(), store the value, return itloader(), store the value, return it| Name | Type | Description |
|---|---|---|
key* | str | Cache key. |
loader* | Callable[[], Awaitable[T]] | Async callable that fetches the value on miss/revalidation. |
fresh_for | timedelta | None | Default: NoneHow long a cached value is considered fresh (no revalidation).
Defaults to |
max_age | timedelta | None | Default: NoneTotal lifetime of a cached entry. After this, the next access
blocks on the loader. Defaults to :data: |
model | type[T] | None | Default: NoneOptional Pydantic model class. When provided, values are
serialized via |