HTTP Cache¶
Collect metrics based on prometheus.
- class blacksmith.middleware._async.http_cache.AsyncAbstractCache(*args, **kwargs)¶
Abstract Redis Client.
- async initialize() Any¶
Initialize the cache
- async get(name: str) Any | None¶
Get a value from cache
- async set(name: str, value: Any, ex: timedelta) bool | None¶
Get a value from cache
- __init__(*args, **kwargs)¶
- __parameters__ = ()¶
- __protocol_attrs__ = {'get', 'initialize', 'set'}¶
- classmethod __subclasshook__(other)¶
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
- class blacksmith.middleware._async.http_cache.AsyncHTTPCacheMiddleware(cache: AsyncAbstractCache, metrics: PrometheusMetrics | None = None, policy: AbstractCachePolicy = <blacksmith.domain.model.middleware.http_cache.CacheControlPolicy object>, serializer: type[AbstractSerializer] = <class 'blacksmith.domain.model.middleware.http_cache.JsonSerializer'>)¶
Http Cache Middleware based on Cache-Control and redis.
- __init__(cache: AsyncAbstractCache, metrics: PrometheusMetrics | None = None, policy: AbstractCachePolicy = <blacksmith.domain.model.middleware.http_cache.CacheControlPolicy object>, serializer: type[AbstractSerializer] = <class 'blacksmith.domain.model.middleware.http_cache.JsonSerializer'>) None¶
- async initialize() None¶
Lazy initialization of a middleware.
For instance, used to initialize connection to storage backend.
- async cache_response(client_name: str, path: str, req: HTTPRequest, resp: HTTPResponse) bool¶
- async get_from_cache(client_name: str, path: str, req: HTTPRequest) HTTPResponse | None¶
- __call__(next: AsyncMiddleware) AsyncMiddleware¶
Call self as a function.
- observe_cache_hit(client_name: str, method: str, path: str, status_code: int, latency: float) None¶
- inc_cache_miss(client_name: str, cachable_state: Literal['uncachable_request', 'uncachable_response', 'cached'], method: Literal['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], path: str, status_code: int) None¶