Skip to content

Http Cache

infra.http_cache

Summary

Domain-agnostic HTTP response cache.


A thin Redis-backed response cache that wraps route handlers as a decorator.

Example
1
2
3
4
5
from infra.http_cache import cache_response

@cache_response(ttl=60)
async def list_entities(request, skip: int = 0, limit: int = 100):
    ...
Notes

The cache is independent of the report cache (apicache:* vs report:* key namespaces) and of Mongo's cached repositories — it operates directly on the endpoint. Cached responses expire via TTL (default settings.cache_ttl). When no http_cache_store is wired on request.app.state.db_resources the decorator is a no-op pass-through, so applying/removing it is safe and trivial.

Classes

HttpCacheStore

1
2
3
4
HttpCacheStore(
    url: str = "redis://localhost:6379/0",
    decode_responses: bool = True,
)

Store string responses in Redis with TTL under the apicache: key space.

Attributes:

Name Type Description
_redis Redis

Asynchronous Redis client bound to the configured URL.

Initialize the store with a Redis connection.

Parameters:

Name Type Description Default
url str

Redis connection URL.

'redis://localhost:6379/0'
decode_responses bool

Whether Redis returns decoded strings.

True
Functions
close async
close() -> None

Close the underlying Redis connection.

delete async
delete(key: str) -> None

Delete the value stored at key.

Parameters:

Name Type Description Default
key str

Cache key to remove.

required
generation async
generation(resource: str) -> str

Return the current generation counter for a resource namespace.

Parameters:

Name Type Description Default
resource str

Resource namespace to read.

required

Returns:

Name Type Description
str str

Generation counter, or "0" when unset.

get async
get(key: str) -> Optional[str]

Read the value stored at key.

Parameters:

Name Type Description Default
key str

Cache key to read.

required

Returns:

Type Description
Optional[str]

Optional[str]: Cached value, or None when absent.

invalidate async
invalidate(*resources: str) -> None

Invalidate all cached responses for the given resource namespaces.

Parameters:

Name Type Description Default
*resources str

Resource namespaces to invalidate.

()
Notes

Bumps each namespace's generation counter so existing entries become unreachable immediately; Redis reclaims them at TTL expiry.

set async
set(key: str, value: str, ttl: int) -> None

Store value at key with a TTL.

Parameters:

Name Type Description Default
key str

Cache key to write.

required
value str

Serialized value to persist.

required
ttl int

Lifetime in seconds.

required

Functions

cache_response

cache_response(ttl: Optional[int] = None) -> Callable

Decorate a GET-style route handler to cache its JSON response.

Parameters:

Name Type Description Default
ttl Optional[int]

Cache lifetime in seconds; defaults to settings.cache_ttl.

None

Returns:

Name Type Description
Callable Callable

Decorator that wraps the route handler.

Notes

The wrapped handler must accept a request: Request parameter (as every openapi_first handler does). Responses that are already a fastapi.Response (for example 204) are returned untouched and never cached. When no http_cache_store is wired on request.app.state.db_resources the decorator passes through to the handler without caching.