Converting an app from redis-py to redis-cluster-py

40 views
Skip to first unread message

massimo....@uniupo.it

unread,
Feb 13, 2017, 10:49:17 AM2/13/17
to Redis DB
Hi,
in the last days I make some progress and now I'm able to run a redis-cluster. Now the problem is how to convert a redis-py app into a redis-cluter-py app.

The app which I like to convert is scrapy-redis (https://github.com/rolando/scrapy-redis).

Right now, I'm practicing with a very simple app (radisqueue) but It is hard to me convert this code in order to have the connection phase like this one:

rom rediscluster import StrictRedisCluster
startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)

After my signature, there is the very simple source code that works well with one simple redis instance. I would like make this app able to interact with a redis-cluster.

Any suggestion is more than welcome.

Thanks,
Massimo

import redis

class RedisQueue(object):
    """Simple Queue with Redis Backend"""
    def __init__(self, name, namespace='queue', **redis_kwargs):
       """The default connection parameters are: host='localhost', port=6379, db=0"""
       self.__db= redis.Redis(**redis_kwargs)
       self.key = '%s:%s' %(namespace, name)

    def qsize(self):
        """Return the approximate size of the queue."""
        return self.__db.llen(self.key)

    def empty(self):
        """Return True if the queue is empty, False otherwise."""
        return self.qsize() == 0

    def put(self, item):
        """Put item into the queue."""
        self.__db.rpush(self.key, item)

    def get(self, block=True, timeout=None):
        """Remove and return an item from the queue.

        If optional args block is true and timeout is None (the default), block
        if necessary until an item is available."""
        if block:
            item = self.__db.blpop(self.key, timeout=timeout)
        else:
            item = self.__db.lpop(self.key)

        if item:
            item = item[1]
        return item

    def get_nowait(self):
        """Equivalent to get(False)."""
        return self.get(False)
Reply all
Reply to author
Forward
0 new messages