Example #1
0
 def _pipeline(self):
     """
     Redis list pipeline getter. A pipeline is a buffer.
     """
     try:
         b = self._pipeline_cache
     except AttributeError:
         r = open_redis_connection()
         b = self._pipeline_cache = r.pipeline()
     return b
Example #2
0
    def iterate(self):
        """
        Iterate over the Redis list.
        Return an iterator object which iterates over `Redis<Provider>Entry` objects.
        """
        r = open_redis_connection()
        pipeline = r.pipeline()

        def _lpop():
            """
            Pop from the head of the Redis list and get the hash entry.
            Convert the pop and hash items to a `Redis<Provider>Entry` instance.
            """
            entry_id = r.lpop(self._list_name)
            if not entry_id:  # The list has been completely consumed.
                return None

            hash_name = '{}:{}'.format(self._list_name, entry_id.decode(encoding='UTF-8'))
            entry_dict = r.hgetall(hash_name)
            # Delete the hash through a pipeline. The use of a pipeline is to avoid a sort
            # of bug. The bug was the following:
            # entry = r.hgetall(hash_name)  -- READ
            # r.delete(hash_name)           -- DELETE
            # Sometimes the DELETE happens before the READ causing the read value to be None: this
            # is very very weird, but it happened sometimes. Using a pipeline solve this.
            pipeline.delete(hash_name)
            return self._init_redis_provider_entry(entry_id, entry_dict)

        def _lpop_mgr():
            """
            Wrap _lpop in a try-except block to make sure the pipeline is executed even in case
            of exception.
            """
            try:
                item = _lpop()
                if not item:
                    pipeline.execute()
                return item
            except:
                pipeline.execute()
                raise

        # The first argument of iter must be a callable, that's why we created the _lpop()
        # closure. This closure will be called for each iteration and the result is returned
        # until the result is None.
        return iter(_lpop_mgr, None)
Example #3
0
def redis_print(args):
    from utils.redis import open_redis_connection
    redis = open_redis_connection()

    if args.bearertoken_id:
        print("Printing Redis keys for bearertoken_id: {}.".format(args.bearertoken_id))
        lists = redis.keys('*token:{}'.format(args.bearertoken_id))
        if not lists:
            sizel = 0
            print("\n * NO LIST")
        # It should be only one list, but you never know...
        for list in lists:
            items = redis.lrange(list, 0 , -1)
            sizel = len(items)
            print("\n * THE LIST {} CONTAINS {} ITEMS:".format(list, sizel))
            print(items)

        hashes = redis.keys('*token:{}:*'.format(args.bearertoken_id))
        if not hashes:
            print("\n * NO HASHES")
        else:
            print("\n * THE HASHES ARE:")
        for hash_ in hashes:
            print(redis.hgetall(hash_))

        print("\n * CHECKS:")
        # Check that there is only 1 list like: *token:x
        size = len(lists)
        msg = 'OK' if size in [1, 0] else 'NOK!!!!!'
        print("{} list(s) found - {}".format(size, msg))
        # Check that the size of the list is = the number of hashes like *token:x:*
        sizeh = len(hashes)
        msg = 'OK' if sizel == sizeh else 'NOK!!!!!'
        print("{} items in the list, {} hashes - {}".format(sizel, sizeh, msg))

    if args.all:
        print(" * PRINTING ALL REDIS KEYS:")
        keys = redis.keys('*')
        for key in keys:
            print(key.decode('utf-8'))
        print("\n * {} KEYS FOUND".format(len(keys)))

    print('\nDone.')
Example #4
0
def redis_flush(args):
    print("Flushing Redis keys...")
    from utils.redis import open_redis_connection
    redis = open_redis_connection()
    redis.flushall()
    print('Done.')