Ejemplo n.º 1
0
class Connection(object):
    """A lightweight connector to Redis database server."""

    def __init__(self, host, port, db):
        self.host = host
        self.port = port
        self._db = db
        self.redis = Redis(host=self.host, port=self.port, db=self._db)
        self.check_connection()

    def check_connection(self):
        try:
            self.redis.randomkey()
            logging.info("Connected to Redis[db:%s] on %s:%s" % (self._db, self.host, self.port), exc_info=False)
        except:
            logging.error("Cannot connect to Redis[db:%s] on %s:%s" % (self._db, self.host, self.port), exc_info=False)

    def close(self):
        """ Close connection """
        self.redis.connection.disconnect()
        logging.info("Connection to Redis on %s:%s closed." % (self.host, self.port), exc_info=False)

    def __del__(self):
        self.close()

    def switch_to(self, to_db):
        """ Switches the connection from a redis db to another """
        """ Closes the connection and establishes it again with a different redis db """
        self.redis.connection.disconnect()
        self.redis = Redis(host=self.host, port=self.port, db=to_db)
        self._db = to_db
        self.check_connection()

    # Here's a set of function that I think you might need to use frequently.
    # You can reach other Redis commands directly from reds.[command] from your Connection subclass.

    def get(self, key):
        return self.redis.get(key)

    def set(self, key, val):
        return self.redis.set(key, val)

    def save(self):
        """ synchronously save the db on disk """
        if self.redis.save():
            logging.info("Redis[db:%s] saved successfully" % self._db, exc_info=False)
        else:
            logging.error("Redis[db:%s] was NOT saved successfully" % self._db, exc_info=True)

    def bgsave(self):
        """ asynchronously save the db on disk """
        if self.redis.bgsave():
            logging.info("Redis[db:%s] saved successfully" % self._db, exc_info=False)
        else:
            logging.error("Redis[db:%s] was NOT saved successfully" % self._db, exc_info=True)

    def last_save(self):
        """ return the unix time stamp of the last successfully saving of the dataset on disk """
        return self.redis.lastsave()
Ejemplo n.º 2
0
curr_ut_cnt = cache.scard(user_todo)
curr_us_cnt = cache.scard(user_set)
curr_expected = curr_us_cnt+curr_ut_cnt
prev_uq_cnt = 0
prev_ut_cnt = 0
prev_us_cnt = 0
prev_expected = 0
try:
    while True:
        print "-"*10, datetime.now(), "-"*20

        prev_us_cnt = curr_us_cnt
        curr_us_cnt = cache.scard(user_set)    
        print "Set:".rjust(8), str(curr_us_cnt).rjust(8), "| delta:", curr_us_cnt-prev_us_cnt

        prev_ut_cnt = curr_ut_cnt
        curr_ut_cnt = cache.scard(user_todo) 
        print "Todo:".rjust(8), str(curr_ut_cnt).rjust(8), "| delta:", curr_ut_cnt-prev_ut_cnt

        prev_uq_cnt = curr_uq_cnt
        curr_uq_cnt = cache.llen(user_q)
        print "Queued:".rjust(8), str(curr_uq_cnt).rjust(8), "| delta:", curr_uq_cnt-prev_uq_cnt
        prev_expected = curr_expected
        curr_expected = curr_us_cnt+curr_ut_cnt
        print "Current Expected Total:", curr_expected, "| delta:", curr_expected-prev_expected
        print "Last save:", cache.lastsave()
        print 
        sleep(30)
except KeyboardInterrupt:
    exit()