Http Cache

Collect metrics based on prometheus.

class blacksmith.domain.model.middleware.http_cache.AbstractCachePolicy

Define the Cache Policy

abstract handle_request(req: HTTPRequest, client_name: str, path: str) bool

A function to decide if the http request is cachable.

abstract get_vary_key(client_name: str, path: str, request: HTTPRequest) str

Create a caching key for the vary part.

abstract get_response_cache_key(client_name: str, path: str, req: HTTPRequest, vary: list[str]) str

Create a caching key for the http response.

abstract get_cache_info_for_response(client_name: str, path: str, req: HTTPRequest, resp: HTTPResponse) tuple[int, str, list[str]]

Return caching info. Tuple (ttl in seconds, vary key, vary list).

class blacksmith.domain.model.middleware.http_cache.AbstractSerializer
abstract static loads(s: str) Any

Load a string to an object

abstract static dumps(obj: Any) str

Get a value from redis

class blacksmith.domain.model.middleware.http_cache.JsonSerializer
static loads(s: str) Any

Load a string to an object

static dumps(obj: Any) str

Get a value from redis

blacksmith.domain.model.middleware.http_cache.int_or_0(val: str) int
blacksmith.domain.model.middleware.http_cache.get_max_age(response: HTTPResponse) int
blacksmith.domain.model.middleware.http_cache.get_vary_header_split(response: HTTPResponse) list[str]
class blacksmith.domain.model.middleware.http_cache.CacheControlPolicy(sep: str = '$')

Initialize the caching using Cache-Control http headers. Also consume the Vary response header to cache response per Vary response headers per request.

Parameters:

sep – Separator used in cache key MUST NOT BE USED in client name.

handle_request(req: HTTPRequest, client_name: str, path: str) bool

A function to decide if the http request is cachable.

get_vary_key(client_name: str, path: str, request: HTTPRequest) str

Create a caching key for the vary part.

get_response_cache_key(client_name: str, path: str, req: HTTPRequest, vary: list[str]) str

Create a caching key for the http response.

get_cache_info_for_response(client_name: str, path: str, req: HTTPRequest, resp: HTTPResponse) tuple[int, str, list[str]]

Return caching info. Tuple (ttl in seconds, vary key, vary list).