Пример #1
0
 async def get_hash_data(self,
                         name,
                         field_name=None,
                         ex=EXPIRED,
                         load_responses=False):
     """
     获取hash对象field_name对应的值
     Args:
         name: redis hash key的名称
         field_name: 获取的hash对象中属性的名称
         ex: 过期时间,单位秒
         load_responses: 结果的键值是否进行load
     Returns:
         反序列化对象
     """
     try:
         if field_name:
             hash_data = await self.redis_db.hget(name, field_name)
             # 返回的键值对是否做load
             if load_responses:
                 with ignore_error():
                     hash_data = ujson.loads(hash_data)
         else:
             hash_data = await self.redis_db.hgetall(name)
             # 返回的键值对是否做load
             if load_responses:
                 rs_data = {}
                 for hash_key, hash_val in hash_data.items():
                     with ignore_error():
                         hash_val = ujson.loads(hash_val)
                     rs_data[hash_key] = hash_val
                 hash_data = rs_data
         if not hash_data:
             # raise RedisClientError("not found hash data, name={}, field_name={}".format(name, field_name))
             hash_data = {}
     except RedisError as e:
         raise RedisClientError(str(e))
     else:
         return hash_data
Пример #2
0
    async def save_update_hash_data(self,
                                    name,
                                    hash_data: dict,
                                    field_name=None,
                                    ex=None,
                                    dump_responses=False):
        """
        获取hash对象field_name对应的值
        Args:
            name: redis hash key的名称
            field_name: 保存的hash mapping 中的某个字段
            hash_data: 获取的hash对象中属性的名称
            ex: 过期时间,单位秒
            dump_responses: 是否对每个键值进行dump
        Returns:
            反序列化对象
        """
        if not isinstance(hash_data, MutableMapping):
            raise ValueError("hash data error, must be MutableMapping.")

        try:
            if not field_name:
                # 是否对每个键值进行dump
                if dump_responses:
                    rs_data = {}
                    for hash_key, hash_val in hash_data.items():
                        if not isinstance(hash_val, str):
                            with ignore_error():
                                hash_val = ujson.dumps(hash_val)
                        rs_data[hash_key] = hash_val
                    hash_data = rs_data

                if not await self.redis_db.hmset(name, hash_data):
                    raise RedisClientError(
                        "save hash data mapping failed, session_id={}".format(
                            name))
            else:
                hash_data = hash_data if isinstance(
                    hash_data, str) else ujson.dumps(hash_data)
                await self.redis_db.hset(name, field_name, hash_data)
            if ex:
                if not await self.redis_db.expire(name, ex):
                    raise RedisClientError(
                        "set hash data expire failed, session_id={}".format(
                            name))
        except RedisError as e:
            raise RedisClientError(str(e))
        else:
            return name
Пример #3
0
    async def get_session(self,
                          session_id,
                          ex=EXPIRED,
                          cls_flag=True,
                          load_responses=False) -> Session:
        """
        获取session
        Args:
            session_id: session id
            ex: 过期时间,单位秒
            cls_flag: 是否返回session的类实例
            load_responses: 结果的键值是否进行load
        Returns:
        """

        try:
            session_data = await self.redis_db.hgetall(session_id)
            if not session_data:
                raise RedisClientError(
                    "not found session, session_id={}".format(session_id))

            if not await self.redis_db.expire(session_id, ex):
                raise RedisClientError(
                    "set session expire failed, session_id={}".format(
                        session_id))
        except RedisError as e:
            aelog.exception("get session error: {}, {}".format(session_id, e))
            raise RedisClientError(e)
        else:
            # 返回的键值对是否做load
            if load_responses:
                hash_data = {}
                for hash_key, hash_val in session_data.items():
                    with ignore_error():
                        hash_val = ujson.loads(hash_val)
                    hash_data[hash_key] = hash_val
                session_data = hash_data

            if cls_flag:
                return Session(account_id=session_data.pop('account_id'),
                               org_id=session_data.pop("org_id"),
                               role_id=session_data.pop("role_id"),
                               permission_id=session_data.pop("permission_id"),
                               **session_data)
            else:
                return session_data
Пример #4
0
    async def get_usual_data(self, name, ex=EXPIRED):
        """
        获取name对应的值
        Args:
            name: redis key的名称
            ex: 过期时间,单位秒
        Returns:
            反序列化对象
        """
        try:
            data = await self.redis_db.get(name)
            if not data:
                raise RedisClientError(
                    "not found usual data, name={}".format(name))

        except RedisError as e:
            raise RedisClientError(str(e))
        else:
            with ignore_error():
                data = ujson.loads(data)
            return data
Пример #5
0
    async def save_session(self,
                           session: Session,
                           dump_responses=False,
                           ex=EXPIRED):
        """
        利用hash map保存session
        Args:
            session: Session 实例
            dump_responses: 是否对每个键值进行dump
            ex: 过期时间,单位秒
        Returns:
        """
        session_data = dict(vars(session))
        # 是否对每个键值进行dump
        if dump_responses:
            hash_data = {}
            for hash_key, hash_val in session_data.items():
                if not isinstance(hash_val, str):
                    with ignore_error():
                        hash_val = ujson.dumps(hash_val)
                hash_data[hash_key] = hash_val
            session_data = hash_data

        try:
            if not await self.redis_db.hmset(session_data["session_id"],
                                             session_data):
                raise RedisClientError(
                    "save session failed, session_id={}".format(
                        session_data["session_id"]))
            if not await self.redis_db.expire(session_data["session_id"], ex):
                raise RedisClientError(
                    "set session expire failed, session_id={}".format(
                        session_data["session_id"]))
        except RedisError as e:
            aelog.exception("save session error: {}, {}".format(
                session.session_id, e))
            raise RedisClientError(str(e))
        else:
            return session.session_id