LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph-sdkcacheswr
    Functionā—Since v0.3

    swr

    Copy
    swr(
      key: str,
      loader: Callable[[], Awaitable[T]],
      *,
      fresh_for:

    Used in Docs

    • Use server-side caching
    View source on GitHub
    timedelta
    |
    None
    =
    None
    ,
    max_age
    :
    timedelta
    |
    None
    =
    None
    ,
    model
    :
    type
    [
    T
    ]
    |
    None
    =
    None
    )
    ->
    SWRResult
    [
    T
    ]

    Parameters

    NameTypeDescription
    key*str

    Cache key.

    loader*Callable[[], Awaitable[T]]

    Async callable that fetches the value on miss/revalidation.

    fresh_fortimedelta | None
    Default:None
    max_agetimedelta | None
    Default:None
    modeltype[T] | None
    Default:None

    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.

    Semantics:

    • cache miss: await loader(), store the value, return it
    • fresh hit (age < fresh_for): return the cached value
    • stale hit (fresh_for <= age < max_age): return the cached value immediately and trigger a best-effort background refresh
    • expired (age >= max_age): await loader(), store the value, return it

    How long a cached value is considered fresh (no revalidation). Defaults to timedelta(0) so every access triggers a background revalidate while still returning the cached value instantly. Values above :data:MAX_CACHE_TTL are clamped to the backend maximum.

    Total lifetime of a cached entry. After this, the next access blocks on the loader. Defaults to :data:MAX_CACHE_TTL (24 h by default). Values above :data:MAX_CACHE_TTL are clamped to the backend maximum.

    Optional Pydantic model class. When provided, values are serialized via model_dump(mode="json") before storage and deserialized via model.model_validate() on read.