base#

class starlite.cache.base.CacheBackendProtocol#

Bases: Protocol

Protocol for cache backends.

async get(key: str) Any#

Retrieve a value from cache corresponding to the given key.

Parameters:

key – name of cached value.

Returns:

Cached value if existing else None.

async set(key: str, value: Any, expiration: int) Any#

Set a value in cache for a given key for a duration determined by expiration.

Parameters:
  • key – key to cache value under.

  • value – the value to be cached.

  • expiration – expiration of cached value in seconds.

Notes

  • expiration is in seconds.

  • return value is not used by Starlite internally.

Returns:

Any

async delete(key: str) Any#

Delete a value from the cache and remove the given key.

Parameters:

key – key to be deleted from the cache.

Notes

  • return value is not used by Starlite internally.

Returns:

Any

__init__(*args, **kwargs)#
class starlite.cache.base.Cache#

Bases: object

Wrapper for a provided CacheBackend that ensures it is called in an async and thread-safe fashion.

This enables the use of normal sync libraries (such as the standard Redis python client) for caching responses.

__init__(backend: CacheBackendProtocol, default_expiration: int, cache_key_builder: CacheKeyBuilder) None#

Initialize Cache.

Parameters:
  • backend – A class instance fulfilling the Starlite CacheBackendProtocol.

  • default_expiration – Default value (in seconds) for cache expiration.

  • cache_key_builder – A function that receives a request object and returns a unique cache key.

async get(key: str) Any#

Proxy CacheBackendProtocol.get().

Parameters:

key – name of cached value.

Returns:

Cached value if existing else None.

async set(key: str, value: Any, expiration: Optional[int] = None) Any#

Proxy CacheBackendProtocol.set().

Parameters:
  • key – key to cache value under.

  • value – the value to be cached.

  • expiration – expiration of cached value in seconds.

Notes

  • expiration is in seconds.

  • return value is not used by Starlite internally.

Returns:

Any

async delete(key: str) Any#

Proxy CacheBackendProtocol.delete().

Parameters:

key – key to be deleted from the cache.

Notes

  • return value is not used by Starlite internally.

Returns:

Any

build_cache_key(request: Request, cache_key_builder: Optional[CacheKeyBuilder]) str#

Construct a unique cache key from the request instance.

Parameters:
  • request – A Request instance.

  • cache_key_builder – An optional CacheKeyBuilder function.

Returns:

A unique cache key string.