Module

cache.deferred

Deferred-value cache for Suspense context values.

DeferredCachestores successful async factory results behind explicit app or route-owned cache instances. On a warm hit it returns the cached value directly; on a miss it returns an awaitable that resolves and stores the value.

Classes

_CachedValue 2

Attributes

Name Type Description
value object
expires_at float
_SharedDeferred 3
Awaitable shared by all consumers for one in-flight cache key.

Awaitable shared by all consumers for one in-flight cache key.

Methods

close 0
Discard an unstarted deferred value. Suspense calls ``close()`` on awaitables …
def close(self) -> None

Discard an unstarted deferred value.

Suspense callsclose()on awaitables when validation fails before scheduling them. Removing the in-flight entry keeps a rejected render from pinning the key forever without poisoning other consumers that already received the same shared awaitable.

Internal Methods 2
__init__ 4
def __init__(self, cache: DeferredCache, key: str, factory: Callable[[], Awaitable[T]], ttl: float) -> None
Parameters
Name Type Description
cache
key
factory
ttl
__await__ 0 Any
def __await__(self) -> Any
Returns
Any
DeferredCache 4
Small explicit TTL cache for Suspense deferred values. Use one cache instance per app, service, or…

Small explicit TTL cache for Suspense deferred values.

Use one cache instance per app, service, or route module. The cache stores successful factory results only. Warm hits return the value synchronously, soSuspenserenders that value in the initial shell. Misses return an awaitable, preserving the existing shell-plus-deferred-OOB Suspense path.

Methods

get_or_defer 3 T | Awaitable[T]
Return a cached value if fresh, otherwise an awaitable for *factory*. ``ttl=No…
def get_or_defer(self, key: str, factory: Callable[[], Awaitable[T]], *, ttl: float | None = None) -> T | Awaitable[T]

Return a cached value if fresh, otherwise an awaitable for factory.

ttl=None uses the cache default. ttl <= 0bypasses both storage and in-flight dedupe, which is useful when a caller wants Suspense deferral without reuse. Exceptions are never cached.

Parameters
Name Type Description
key
factory
ttl Default:None
Returns
T | Awaitable[T]
delete 1
Remove a cached or in-flight value if present.
def delete(self, key: str) -> None
Parameters
Name Type Description
key
clear 0
Remove all cached and in-flight values.
def clear(self) -> None
Internal Methods 1
__init__ 1
def __init__(self, *, default_ttl: float = 300.0) -> None
Parameters
Name Type Description
default_ttl Default:300.0