Exemple #1
0
    def encode(self, data: object):
        """
        convert given data into redis token

        Args:
        * [data(any)] data can be anything, bytes, int, float or whatever

        Returns:
        * [token(bytes)] composed token consisted of data, type, timestamp
        """
        # reset
        self.reset()

        if data is None:
            raise excepts.NullPointerException("data cannot be a null type")

        # 1. create timestamp
        self.timestamp = ticker.time_since1970()

        # 2. assign bytes to self.data
        if type(data) is bytes:
            self.dtype = "bytes"
            self.data = convert.binary_to_string(base64.b64encode(data))
        else:  # other types
            self.data = data

            if type(data) is int:
                self.dtype = "int"
            elif type(data) is float:
                self.dtype = "float"
            elif type(data) is str:
                self.dtype = "str"
            elif type(data) is list:
                self.dtype = "list"
            elif type(data) is dict:
                self.dtype = "dict"
            else:
                raise excepts.InvalidParamException(
                    "{} cannot be processed into string nor bytes".format(
                        type(data)))

        # 3. composing a token dictionary type
        self.token = {
            "data": self.data,
            "type": self.dtype,
            "timestamp": self.timestamp
        }

        # return to caller
        return convert.dict_to_binary(self.token)
Exemple #2
0
def reflect_request_with_cache_callback(cache: object, do_action: object,
                                        *args):
    """
    用于缓存池的回调函数

    @Args:
    * [cache] 用于访问的缓存
    * [do_action] 回调函数, 函数结构为callback(conn, [args]), 如果不需要附加参数,那么回调函数只为callback(conn)
    * [args] 用户需要添加的参数
    """
    if cache is None:
        raise Exceptions.NullPointerException("cache is not available")

    if args is None:
        return do_action(cache)
    else:
        return do_action(cache, args)
Exemple #3
0
    def decode(self, token: bytes):
        """
        since obtained token from redis, use this method to 
        convert the bytes into correct data type

        Args:
        * [token(bytes)] obtained bytes from redis

        Returns:
        * [token(dict)] contains data, timestamp, type infomation
        """
        # reset all
        self.reset()

        if token is not None:
            # 1, convert the bytes into dictionary
            self.token = convert.binary_to_dict(token)

            # 2. assign to attributes
            self.dtype = self.token["type"]
            self.timestamp = self.token["timestamp"]

            # 3. if bytes, process them carefully
            if self.dtype == "bytes":
                self.data = base64.b64decode(
                    convert.string_to_binary(self.token["data"]))
            else:  # normal data
                self.data = self.token["data"]

            # 4. return to caller
            self.token = {
                "data": self.data,
                "type": self.dtype,
                "timestamp": self.timestamp
            }
            return self.token

        else:
            raise excepts.NullPointerException("token cannot be a null type")
Exemple #4
0
def reflect_request_with_all_callback(database: PySQLPool, cache: object,
                                      do_action: object, *args):
    """
    用于数据库连接池自释放,提供一个可供用户使用的数据库连接和回调函数

    @Args:
    * [database] 用于访问的数据库
    * [cache] 用于访问的缓存
    * [do_action] 回调函数, 函数结构为callback(conn, [args]), 如果不需要附加参数,那么回调函数只为callback(conn)
    * [args] 用户需要添加的参数
    """
    if not isinstance(database, PySQLPool):
        raise Exceptions.NoAvailableResourcesFoundException(
            "database is not available")

    if cache is None:
        raise Exceptions.NullPointerException("cache is not available")

    conn = None

    try:
        # get connection from pool
        conn = database.get_connection()

        # check the connection status
        if not PySQLConnection.check_connection(conn):
            PySQLConnection.reconnect(conn)

        # send database connection to callback function
        if args is None:
            return do_action(conn, cache)
        else:
            return do_action(conn, cache, args)

    finally:
        if conn:
            database.put_connection(conn)