示例#1
0
class KeyValueStorage:

    """
    Key value storage with redis server
    """

    def __init__(self, host: str, port: int, retries_limit: int, timeout: int):

        self.host = host
        self.port = port
        self.timeout = timeout
        self.retries_limit = retries_limit
        self._kv_storage = None
        self._connect()

    def _connect(self) -> NoReturn:

        """
        Connection to redis server with retries
        :param retry_counter: counter of times of retrying
        :return: Redis connection if it was successful and None otherwise
        """

        try:
            self._kv_storage = Redis(
                host=self.host,
                port=self.port,
                socket_timeout=self.timeout
            )
            self._kv_storage.ping()
        except Exception:
            logging.error(
                "Got error while connecting to redis with host %s and port %d.",
                self.host,
                self.port
            )

    def _reset_connection(self) -> NoReturn:

        """
        Resets connection to redis server
        :return:
        """

        self._kv_storage.close()
        self._connect()

    @make_retries
    def get(self, key: str) -> str:

        """
        Gets value by key as from persistent data storage
        :param key: key to get value for
        :return: value for specified key
        """

        result = self._kv_storage.get(key)

        return result.decode("utf-8") if result is not None else result

    def cache_get(self, key: str) -> Optional[float]:

        """
        Gets value by key from cache
        :param key: key to get value for
        :return: float value if found some in cache and None otherwise
        """

        result = None
        try:
            result = self.get(key)
        except (ConnectionError, TimeoutError) as exception:
            logging.error(
                "Could not get value from cache. Encountered error: %s",
                str(exception)
            )

        return float(result) if result is not None else result

    @make_retries
    def _set(self, key: str,
             value: Union[float, int],
             key_expire_time_sec: int) -> NoReturn:

        """
        Sets the value into cache by specified key
        :param key: key to set value for
        :param value: value for setting
        :param key_expire_time_sec: time in which key will be expired
        """

        self._kv_storage.set(key, str(value), ex=key_expire_time_sec)

    def cache_set(self, key: str,
                  value: Union[float, int],
                  key_expire_time_sec: int) -> NoReturn:

        """
        Sets the value into cache by specified key
        :param key: key to set value for
        :param value: value for setting
        :param key_expire_time_sec: time in which key will be expired
        """

        try:
            self._set(key, str(value), key_expire_time_sec=key_expire_time_sec)
        except (ConnectionError, TimeoutError) as exception:
            logging.error(
                "Could not set value to cache. Encountered error: %s",
                str(exception)
            )

    def clear(self) -> NoReturn:
        self._kv_storage.flushall()