get_or_set¶
-
async
AsyncCacheAdapter.get_or_set(key: str, value: Union[Any, callable, Coroutine, Awaitable], timeout: int = 300) → Any[source]¶ Attempt to return the value of
keyin the cache. Ifkeydoesn’t exist or is expired, then it will be set tovalue, andvaluewill be returned.The
valueparameter can be any standard type such asstrordict- or it can be a callable function / method which returns the value to set and return.Basic Usage:
>>> c = CacheAdapter() >>> c.get('testing') None >>> c.get_or_set('testing', 'hello world') 'hello world' >>> c.get('testing') 'hello world'
Set and get the value from a function if ``key`` didn’t exist / was expired:
>>> def my_func(key): return "hello {} world".format(key) >>> c = CacheAdapter() >>> c.get_or_set('example', my_func) 'hello example world' >>> c.get('example') 'hello example world'
- Parameters
- Return Any value
The value of the cache key
key, orvalueif it wasn’t found.