get_or_set_async

async CacheAdapter.get_or_set_async(key: str, value: Union[Any, callable, Coroutine, Awaitable], timeout: int = 300) → Any[source]

Async coroutine compatible version of get_or_set().

Example with Async function:

>>> async def my_coro(key): return f"hello {key} world"
>>> c = CacheAdapter()
>>> await c.get_or_set_async('coro_example', my_coro)
'hello example world'
>>> c.get('coro_example')
'hello example world'

Also works with non-async functions:

>>> def my_func(key): return f"hello {key} world"
>>> await c.get_or_set_async('func_example', my_func)
'hello example world'
>>> c.get('func_example')
'hello example world'
Parameters
  • key (str) – The cache key (as a string) to get/set the value for, e.g. example:test

  • value (Any) – The value to store in the cache key key. Can be a standard type, a coroutine / awaitable, or a plain callable function.

  • timeout (int) – The amount of seconds to keep the data in cache. Pass None to disable expiration.

Return Any value

The value of the cache key key, or value if it wasn’t found.