Skip to content

Instantly share code, notes, and snippets.

@abduakhatov
Created December 8, 2022 15:10
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 abduakhatov/cccfbd231eb3da8f8e483fa7c924c729 to your computer and use it in GitHub Desktop.
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.
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