Esempio n. 1
0
 def pop(self, key, *args, **kwargs):
     """ 从队列右边 pop 出元素
     :param key: 队列名
     :return:
     """
     try:
         result = self.server.exec('brpop', key, *args, **kwargs)
         item = get_index_arr(get_index_arr(result, default=[]))
     except Exception as e:
         self.logger.warning("<brpop> pop {}".format(key, e.args))
         item = None
     return item
Esempio n. 2
0
    def filter_add(self, key, *args):
        """ 判断是否存在是正对redis set的操作
        :param key: 输入的key
        :param args: 需要存入的数据
        """
        if len(args) > 255:
            raise Exception("args greater 255")

        __key = self.__format_key(key)
        result = self.server.exec('hmset',
                                  __key,
                                  format_dict=self.__init_hash_table(*args))
        return get_index_arr(result)
Esempio n. 3
0
    def filter_is_exist(self, key, *args):
        """ 判断是否存在是正对redis set的操作
        :param key: 输入的key
        :param args: 输入的值
        :return:
        """
        if len(args) > 255:
            raise Exception("args greater 255")

        __key = self.__format_key(key)
        result = self.server.exec('hmget', __key, *[gen_md5(x) for x in args])
        return [
            True if bytes_to_str(x) else False
            for x in get_index_arr(result, default=[])
        ]
Esempio n. 4
0
    def move_all(self, key1, key2, *args, **kwargs):
        """ 把key1的所有元素移动到key2里面

        元素的移动使用 brpoplpush,因为,考虑到事务性
        :param key1:
        :param key2:
        :return:
        """
        try:
            while bool(
                    get_index_arr(self.server.exec('exists', key1),
                                  default=False)):
                self.move_pop(key1, key2, *args, **kwargs)
        except Exception as e:
            self.logger.warning("<move_all> move_all {} to {} {}".format(
                key1, key2, e.args))
        return True
Esempio n. 5
0
 def move_pop(self, key1, key2, *args, **kwargs):
     """ 从 key1 的右边 pop 出一个元素放入到 key2 的左边
     :param key1:
     :param key2:
     :return:
     """
     try:
         result = self.server.exec('brpoplpush',
                                   key1,
                                   key2,
                                   timeout=0,
                                   *args,
                                   **kwargs)
         item = get_index_arr(result)
     except Exception as e:
         self.logger.warning("<brpoplpush> move pop {} to {} {}".format(
             key1, key2, e.args))
         item = None
     return item
Esempio n. 6
0
 def __init__(self, *args, **kwargs):
     self.filter_cls = get_index_arr(args, default={}).get(
         "FILTER_CLS", FILTER_CLS)(*args, **kwargs)
     super(QueuePipeline, self).__init__(*args, **kwargs)