def METHOD_NAME(f, maxsize=MAXSIZE):
    r"""Decorator used to memoize a generator.
    The standard approach of using ``functools.lru_cache``
    cannot be used, as it only memoizes the generator
    object, not the results of the generator.
    See https://stackoverflow.com/a/10726355/10958457 for the
    original implementation.
    Args:
        f (function or generator): function or generator to
            memoize
        maxsize (int): positive integer that defines
            the maximum size of the cache
    Returns:
        function or generator: the memoized function or generator
    """
    cache = LimitedSizeDict(maxsize=maxsize)
    @functools.wraps(f)
    def ret(*args):
        if args not in cache:
            cache[args] = f(*args)
        if isinstance(cache[args], (GeneratorType, Tee)):
            # the original can't be used any more,
            # so we need to change the cache as well
            cache[args], r = tee(cache[args])
            return r
        return cache[args]
    return ret