mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add singleton meta with no args
This commit is contained in:
parent
9d08ae91ee
commit
b26ce25719
@ -16,3 +16,21 @@ class SingletonMeta(type):
|
|||||||
instance = super().__call__(*args, **kwargs)
|
instance = super().__call__(*args, **kwargs)
|
||||||
cls._instances[cls] = instance
|
cls._instances[cls] = instance
|
||||||
return cls._instances[cls]
|
return cls._instances[cls]
|
||||||
|
|
||||||
|
|
||||||
|
class SingletonMetaNoArgs(type):
|
||||||
|
"""
|
||||||
|
Singleton metaclass for classes without parameters on constructor,
|
||||||
|
for compatibility with FastApi Depends() function.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_instances = {}
|
||||||
|
|
||||||
|
_lock: Lock = Lock()
|
||||||
|
|
||||||
|
def __call__(cls):
|
||||||
|
with cls._lock:
|
||||||
|
if cls not in cls._instances:
|
||||||
|
instance = super().__call__()
|
||||||
|
cls._instances[cls] = instance
|
||||||
|
return cls._instances[cls]
|
Loading…
x
Reference in New Issue
Block a user