hjelpmig
hjelpmig

Reputation: 1465

what is the best way to implement communication with redis in python

I am trying to write a module that communicates with redis. So far its doing following things.

  1. Get the token
  2. if token A then connect to redis A
  3. else connect to redis B
  4. get data for a specific key
  5. delete a key

Here is what I have written:

import redis

def get_data(token):

    if token == "tokenA" 
        connection = redis.Redis(connection_pool=name_of_redis_engine1)
    else:   
        connection = redis.Redis(connection_pool=name_of_redis_engine2)

    data = connection.hgetall(token)
    if not data:
        raise Some Error

    return data

def delete_data(token):

    connection = redis.Redis(connection_pool=name_of_redis_engine)

    data = redis_connection.delete(token)
    if not data:
        raise Some Error

    return data

Since, there is a some duplicate data in both functions which is not a good approach. I was wondering about whats the best way to make it neat Maybe class? . Will appreciate any help alot.

Upvotes: 3

Views: 502

Answers (3)

Thomas Fenzl
Thomas Fenzl

Reputation: 4392

You can make it a class. Given your exact requirements, what you can do is:

class RedisStore:
    def __init__(self, default_connection, tokenA_connection):
        self._default_connection = default_connection
        self._tokenA_connection = tokenA_connection 

    def _chose_connection(token):
        if token == "tokenA" 
            return self._tokenA_connection
        else:   
            return self._default_connection

    def get_data(self, token):
        connection = self._chose_connection(token)
        data = connection.hgetall(token)
        if not data:
            raise Exception("Some Error")  # you can only raise exceptions, and you should use a more specific one

        return data

    def delete_data(self, token):
        connection = self._chose_connection(token)
        data = connection.delete(token)
        if not data:
            raise Exception("Some Error")  # if that is supposed to raise the same exception, you could generalize further...
        return data

redis_store = new RedisStore(redis.Redis(connection_pool=name_of_redis_engine1), redis.Redis(connection_pool=name_of_redis_engine2))

You can instantiate the class once and reuse it for multiple lookups/deletes.

Upvotes: 1

hjelpmig
hjelpmig

Reputation: 1465

Thankyou @yarkee. I have come up with the following solution. Will appreciate if anyone can point out some more better way.

class RedisClass(object):
def __init__(self, token=None):

    self.token = token

    if self.token == "TokenA":
        self.redis_connection = redis.Redis(connection_pool="RedisEngineA")
    else:
        self.redis_connection = redis.Redis(connection_pool="RedisEngineB")

def get_data(self):
    data = self.redis_connection.hgetall(self.token)
    if not data:
        raise AuthenticationError({"status: Invalid token"}, code=200, log_error=False)
    return data

def delete_data(self):
    data = self.redis_connection.delete(self.token)
    if not data:
        raise AuthenticationError({"status: Invalid token"}, code=200, log_error=False)
    return data

Upvotes: 0

Yarkee
Yarkee

Reputation: 9424

Make it be a class.

import redis

class RedisOp(object):
    def __init__(self):
        self.connection = redis.Redis(connection_pool=name_of_redis_engine)

    def get_data(self, token):
        data = self.connection.hgetall(token)
        if not data:
            raise Some Error
        return data

    def delete_data(self, token):
        data = self.connection.delete(token)
        if not data:
            raise Some Error
        return data


op = RedisOp()
print op.get_data('mykey')
op.delete_data('mykey')

Upvotes: 0

Related Questions