def METHOD_NAME(mode: str = "w") -> Generator[IO, None, None]:
    if "a" in mode:
        raise ValueError("Appending to cache files is not supported")
    self._reserve_space(size)
    temp_file = NamedTemporaryFile(dir=self._temp_dir, mode=mode, delete=False)
    try:
        yield temp_file
        temp_file.close()
        path.parent.mkdir(parents=True, exist_ok=True)
        os.replace(temp_file.name, path)
    except Exception:
        os.remove(temp_file.name)
        raise