Skip to content

Instantly share code, notes, and snippets.

@abduakhatov
Created December 8, 2022 15:10
Show Gist options
  • Save abduakhatov/cccfbd231eb3da8f8e483fa7c924c729 to your computer and use it in GitHub Desktop.
Save abduakhatov/cccfbd231eb3da8f8e483fa7c924c729 to your computer and use it in GitHub Desktop.

Revisions

  1. abduakhatov created this gist Dec 8, 2022.
    22 changes: 22 additions & 0 deletions redis_cli.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@

    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)