get_or_set¶
-
CacheAdapter.
get_or_set
(key: str, value: Union[Any, callable], timeout: int = 300) → Any[source]¶ Attempt to return the value of
key
in the cache. Ifkey
doesn’t exist or is expired, then it will be set tovalue
, andvalue
will be returned.The
value
parameter can be any standard type such asstr
ordict
- 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
, orvalue
if it wasn’t found.