1

I'm doing multiple API calls but in each API call I'm checking if redis connection is available. showing only one of the API calls below.

def check_redis_conn():

    redis_exists = RedisCache.is_redis_available()
    if redis_exists is True:
        set_Data = RedisCache.set_data("message", "This is redis Test")
        return True
    else:
        return redis_exists


def hello_vortex(request):
    is_redis_connected = check_redis_conn()
    if is_redis_connected is True:
        data = {'Description': 'This is the sample API call', 'version': 1.0}
        return JsonResponse{data,safe=False}
    else:
        return is_redis_connected

RedisCache class:

@classmethod
    def set_connect(cls):
        try:
            redis_instance = redis.StrictRedis(
            host='127.0.0.1',
            port=6379,
            )

    @classmethod
    def get_conn(cls):
        try:
            cls.redis_instance = cls.set_connect()
            return cls.redis_instance
        except Exception as e:
            return JsonResponse{'message':'Error while connecting', safe=False}

    @classmethod
    def is_redis_available(cls):
        try:
            isConn = cls.get_conn()
            isConn.ping()
            return True
        except Exception as e:
            return JsonResponse{'message':'Error while connecting', safe=False}

How can I check the redis connection only once when the server starts and return the Json Response on the server if the connection is not available.

ninjacode
  • 318
  • 2
  • 18

1 Answers1

0

You can ping the server and check it. If you are using any client, jedis or redison you have ping method to check the connection

Suhas Kashyap
  • 398
  • 3
  • 14