r_cache_async

privex.helpers.decorators.r_cache_async(cache_key: Union[str, callable], cache_time=300, format_args: list = None, format_opt: privex.helpers.decorators.FormatOpt = <FormatOpt.POS_AUTO: 'force_pos'>, **opts) → Any[source]

Async function/method compatible version of r_cache() - see docs for r_cache()

You can bypass caching by passing r_cache=False to the wrapped function.

Basic usage:

>>> from privex.helpers import r_cache_async
>>> @r_cache_async('my_cache_key')
>>> async def some_func(some: int, args: int = 2):
...     return some + args
>>> await some_func(5, 10)
15

>>> # If we await some_func a second time, we'll get '15' again because it was cached.
>>> await some_func(2, 3)
15

Async cache_key generation (you can also use normal synchronous functions/lambdas):

>>> from privex.helpers import r_cache_async
>>>
>>> async def make_key(name, title):
...     return f"mycache:{name}"
...
>>> @r_cache_async(make_key)
... async def who(name, title):
...     return "Their name is {title} {name}"
...
Parameters
  • format_opt (FormatOpt) – (default: FormatOpt.POS_AUTO) “Format option” - how should args/kwargs be used when filling placeholders in the cache_key (see comments on FormatOption)

  • format_args (list) – A list of positional arguments numbers (e.g. [0, 1, 2]) and/or kwargs ['x', 'y', 'z'] that should be used to format the cache_key

  • cache_key (str) – The cache key to store the cached data into, e.g. mydata

  • cache_time (int) – The amount of time in seconds to cache the result for (default: 300 seconds)

  • whitelist (bool) – (default: True) If True, only use specified arg positions / kwarg keys when formatting cache_key placeholders. Otherwise, trust whatever args/kwargs were passed to the func.

Return Any res

The return result, either from the wrapped function, or from the cache.