Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active May 27, 2023 00:28
Show Gist options
  • Select an option

  • Save bryanchow/4fd80e38a75be3c4e64560de2e7fd76e to your computer and use it in GitHub Desktop.

Select an option

Save bryanchow/4fd80e38a75be3c4e64560de2e7fd76e to your computer and use it in GitHub Desktop.

Revisions

  1. bryanchow revised this gist May 27, 2023. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions cache_buster.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # Django cache buster view
    # https://gist.github.com/bryanchow/4fd80e38a75be3c4e64560de2e7fd76e

    from django.core.cache import cache
    @@ -7,12 +8,12 @@

    def bust_cache(request, path):
    """
    A Django view that clears the cached response for the passed-in path, and
    redirects to that path.
    A simple Django view that clears the cached response for the passed-in
    path, and redirects to that path.
    Example URL configuration:
    path("bust/<path:path>", cache_buster.bust_cache, name="bust_cache")
    path("reload/<path:path>", cache_buster.bust_cache, name="bust_cache")
    """

  2. bryanchow revised this gist May 27, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion cache_buster.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # https://gist.github.com/bryanchow/4fd80e38a75be3c4e64560de2e7fd76e

    from django.core.cache import cache
    from django.utils.cache import get_cache_key
    from django.shortcuts import redirect
    @@ -22,4 +24,4 @@ def bust_cache(request, path):
    if cache.has_key(key):
    cache.delete(key)

    return redirect(path)
    return redirect(path)
  3. bryanchow created this gist May 27, 2023.
    25 changes: 25 additions & 0 deletions cache_buster.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from django.core.cache import cache
    from django.utils.cache import get_cache_key
    from django.shortcuts import redirect


    def bust_cache(request, path):
    """
    A Django view that clears the cached response for the passed-in path, and
    redirects to that path.
    Example URL configuration:
    path("bust/<path:path>", cache_buster.bust_cache, name="bust_cache")
    """

    if not path.startswith("/"):
    path = "/{}".format(path)

    request.path = path
    key = get_cache_key(request)
    if cache.has_key(key):
    cache.delete(key)

    return redirect(path)