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

Code before:


def purge_urls_from_cache(urls, backend_settings=None, backends=None):
    """
    Purge multiple URLs from the frontend cache.

    :param urls: An iterable of URLs to purge from the cache.
    :type urls: iterable of str
    :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 referencing specific backends from ``settings.WAGTAILFRONTENDCACHE`` or provided as ``backend_settings``. Can be used to limit purge operations to specific backends.
    :type backends: list, optional

    This function purges multiple URLs from the configured frontend cache backends. It's useful
    when you need to invalidate the cache for multiple URLs at once.

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

    NOTE: This function also handles internationalization, creating language-specific URLs if
    ``WAGTAILFRONTENDCACHE_LANGUAGES`` is set and ``USE_I18N`` is ``True``.
    """
    if not urls:
        return

    backends = get_backends(backend_settings, backends)

    # If no backends are configured, there's nothing to do
    if not backends:
        return

    # Convert each url to urls one for each managed language (WAGTAILFRONTENDCACHE_LANGUAGES setting).
    # The managed languages are common to all the defined backends.
    # This depends on settings.USE_I18N
    # If WAGTAIL_I18N_ENABLED is True, this defaults to WAGTAIL_CONTENT_LANGUAGES
    wagtail_i18n_enabled = getattr(settings, "WAGTAIL_I18N_ENABLED", False)
    content_languages = get_content_languages() if wagtail_i18n_enabled else {}
    languages = getattr(
        settings, "WAGTAILFRONTENDCACHE_LANGUAGES", list(content_languages.keys())
    )
    if settings.USE_I18N and languages:
        langs_regex = "^/(%s)/" % "|".join(languages)
        new_urls = []

        # Purge the given url for each managed language
        for isocode in languages:
            for url in urls:
                up = urlsplit(url)
                new_url = urlunsplit(
                    (
                        up.scheme,

Review comment (performance):
I don't think this should be an opaque "cache object" - the `get_full_url` implementation definitely heavily assumes it's a request.

Code after:

    :param urls: An iterable of URLs to purge from the cache.
    :type urls: iterable of str
    :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 referencing specific backends from ``settings.WAGTAILFRONTENDCACHE`` or provided as ``backend_settings``. Can be used to limit purge operations to specific backends.
    :type backends: list, optional

    This function purges multiple URLs from the configured frontend cache backends. It's useful
    when you need to invalidate the cache for multiple URLs at once.

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

    NOTE: This function also handles internationalization, creating language-specific URLs if
    ``WAGTAILFRONTENDCACHE_LANGUAGES`` is set and ``USE_I18N`` is ``True``.
    """
    from .tasks import purge_urls_from_cache_task

    if not urls:
        return

    purge_urls_from_cache_task.enqueue(list(urls), backend_settings, backends)


def _get_page_cached_urls(page, cache_object=None):
    page_url = page.get_full_url(cache_object)
    if page_url is None:  # nothing to be done if the page has no routable URL
        return []

    return [
        page_url + path.lstrip("/")
        for path in page.specific_deferred.get_cached_paths()
    ]


def purge_page_from_cache(
    page, backend_settings=None, backends=None, *, cache_object=None
):
    """
    Purge a single page from the frontend cache.

    :param page: The page to purge from the cache.
    :type page: 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 referencing specific backends 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, but strongly recommended when making a series of requests to this method. An object to be passed to URL-related methods, allowing cached site root path data to be reused across multiple requests.
    :type cache_object: object, optional