Repository: wagtail/wagtail
File: wagtail/contrib/frontend_cache/utils.py (Python)

Code before:
    ]


def purge_page_from_cache(page, backend_settings=None, backends=None, *, request=None):
    urls = _get_page_cached_urls(page, request=request)
    purge_urls_from_cache(urls, backend_settings, backends)


def purge_pages_from_cache(
    pages, backend_settings=None, backends=None, *, request=None
):
    batch = PurgeBatch(request=request)
    batch.add_pages(pages)
    batch.purge(backend_settings, backends)


class PurgeBatch:
    """Represents a list of URLs to be purged in a single request"""

    def __init__(self, urls=None, *, request=None):
        self.urls = set()

        if urls is not None:
            self.add_urls(urls)

        self.request = request or get_dummy_request()

    def add_url(self, url):
        """Adds a single URL"""
        self.urls.add(url)

    def add_urls(self, urls):
        """
        Adds multiple URLs from an iterable

        This is equivalent to running ``.add_url(url)`` on each URL
        individually
        """
        self.urls.update(urls)

    def add_page(self, page):
        """
        Adds all URLs for the specified page

        This combines the page's full URL with each path that is returned by
        the page's `.get_cached_paths` method
        """
        self.add_urls(_get_page_cached_urls(page, request=self.request))

    def add_pages(self, pages):
        """

Review comment (bug):
Question: Because `get_full_url` is used, could using this request result in the wrong site being used to resolve the URLs hostname?

Code after:
    """
    urls = _get_page_cached_urls(page, cache_object)
    purge_urls_from_cache(urls, backend_settings, backends)


def purge_pages_from_cache(
    pages, backend_settings=None, backends=None, *, cache_object=None
):
    """
    Purge multiple pages from the frontend cache.

    :param pages: An iterable of pages to purge from the cache.
    :type pages: iterable of Page
    :param backend_settings: Optional custom backend settings to use instead of those defined in ``settings.WAGTAILFRONTENDCACHE``.
    :type backend_settings: dict, optional
    :param backends: Optional list of strings matching keys from ``settings.WAGTAILFRONTENDCACHE`` or provided as ``backend_settings``. Can be used to limit purge operations to specific backends.
    :type backends: list, optional
    :param cache_object: Optional object to be passed to URL-related methods, to allow cached site root path data to be reused across multiple requests to this method. If not provided, the ``PurgeBatch`` object created by this method will be used instead.
    :type cache_object: object, optional

    This function retrieves all cached URLs for the given pages and purges them from the configured
    backends. It's useful when you need to invalidate the cache for multiple pages at once,
    for example after a bulk update operation.

    If no custom backends or settings are provided, it will use the default configuration
    from ``settings.WAGTAILFRONTENDCACHE``.

    The `cache_object` parameter can be any kind of object that supports arbitrary attribute
    assignment, such as a Python object or Django Model instance.
    """
    batch = PurgeBatch(cache_object=cache_object)
    batch.add_pages(pages)
    batch.purge(backend_settings, backends)


class PurgeBatch:
    """Represents a list of URLs to be purged in a single request"""

    def __init__(self, urls=None, *, cache_object=None):
        self.urls = set()

        if urls is not None:
            self.add_urls(urls)

        self.cache_object = cache_object

    def add_url(self, url):
        """Adds a single URL"""
        self.urls.add(url)

    def add_urls(self, urls):