<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_cache_dir(directory):
 """Set the directory to cache JSON responses from most API endpoints. """

    global cache_dir

    if directory is None:
        cache_dir = None
        return

    if not os.path.exists(directory):
        os.makedirs(directory)
    if not os.path.isdir(directory):
        raise ValueError("not a directory")
    cache_dir = directory