Skip to content

Instantly share code, notes, and snippets.

@mdespriee
Created January 23, 2022 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdespriee/55b42fc4dc7396b9c55686f9bf714f39 to your computer and use it in GitHub Desktop.
Save mdespriee/55b42fc4dc7396b9c55686f9bf714f39 to your computer and use it in GitHub Desktop.
decorator meant to build singletons, and designed for async coroutines
def async_cache(func):
"""
decorator meant to build singletons, and designed for async coroutines
:param func:
:return:
"""
instances = {}
@functools.wraps(func)
async def wrapper(*args, **kwargs):
if func not in instances:
instances[func] = await func(*args, **kwargs)
return instances[func]
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment