Created
December 8, 2022 15:10
-
-
Save abduakhatov/cccfbd231eb3da8f8e483fa7c924c729 to your computer and use it in GitHub Desktop.
Metaclass for creating classes that allow only a single instance to be created.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MetaSingleton(type): | |
"""Metaclass for creating classes that allow only a single instance to be created.""" | |
_instances = {} | |
def __call__(cls, *args, **kwargs): | |
if cls not in cls._instances: | |
cls._instances[cls] = super().__call__(*args, **kwargs) | |
return cls._instances[cls] | |
class FastApiRedisCache(metaclass=MetaSingleton): | |
"""Communicates with Redis server to cache API response data.""" | |
redis: client.Redis = None | |
def init(self) -> None: | |
self.redis = redis.from_url(host_url) | |
if redis_client.ping(): | |
return (RedisStatus.CONNECTED, redis_client) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment