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
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
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
delete
async
Delete the value stored at key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
Cache key to remove. |
required |
generation
async
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 |
get
async
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 |
invalidate
async
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
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
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
|
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.