Package: django-cache-headers
Django Cache HeadersOverviewDjango Cache Headers allows you to set HTTP caching headers for URL patterns
according to certain policies. It does not perform any caching itself - it
merely sets the headers on the response which are then interpreted by eg. Varnish.Doing a truly zero-conf Varnish turned out to be fragile, so Django Cache
Headers now generates a VCL file that can be included into or adapted to your
default Varnish configuration file.InstallationInstall or adddjango-cache-headersto your Python path.Addcache_headersto yourINSTALLED_APPSsetting.Addcache_headers.middleware.CacheHeadersMiddlewarebefore
SessionMiddleware and AuthenticationMiddleware and MessageMiddleware to yourMIDDLEWARE_CLASSESsetting.PoliciesDjango Cache Headers provides four caching policies. You may define your own policies.:all-users - response is marked as cached once for all users.anonymous-only - response is marked as cached once only for anonymous users.anonymous-and-authenticated - response is marked as cached once for anonymous users and once for authenticated users.per-user - response is marked as cached once for anonymous users and for each authenticated user individually.SettingsThetimeoutskey combines the policy, timeout in seconds and URL regexes in a nested dictionary:CACHE_HEADERS = {
    "timeouts": {
        "all-users": {
            60: (
                "^/all-users/",
            )
        },
        "anonymous-only": {
            60: (
                "^/anonymous-only/",
            )
        },
        "anonymous-and-authenticated": {
            60: (
                "^/anonymous-and-authenticated/",
            )
        },
        "per-user": {
            60: (
                "^/per-user/",
            )
        },
        "custom-policy": {
            60: (
                "^/custom-policy/",
            )
        }
    }
}Setbrowser-cache-secondsto specify how long the browser may cache a
response before it has to revalidate with the server. It defaults to 5 seconds.:CACHE_HEADERS = {"browser-cache-seconds": 10}Setenable-tampering-checksto enable checks that guard against cache
poising by tampering with the cookies.
Keep this disabled for most unit tests. Unit test’s client.login() does not
trigger the normal expected login path.CACHE_HEADERS = {“enable-tampering-checks”: True}Varnish configurationGenerate the VCL snippet:python manage.py generate_vcl > /path/to/generated.vclSave the contents ofsample.vclas/etc/varnish/default.vcl.
Restart Varnish for the configuration to take effect.AuthorsPraekelt ConsultingHedley RoosAltus BarryChangelog0.4Django 2.0 and Python 3 compatibility. Django 1.9 support has been dropped.0.3.3Fix anonymous-only policy to consider isauthenticated cookie.0.3.2Ensure isauthenticated cookie expires at end of session if session is set to do so.0.3.1An anonymous user may in fact have a session. Handle this case gracefully.0.3Added vcl generation management command, to be used in tandem with varnish. sample.vcl updated to reflect usage.Make use of on_user_auth_event to ensure no-cache header is set during login and logout.Extra protection against tampered session cookie.Policies no longer makes an assumption on the session cookie name.0.2.2Iterate over regexes in order of most specific (longest) to least specific (shortest).Revert OrderedDict change since it is not required anymore due to the above change.0.2.1Use an OrderedDict for guaranteed policy iteration order.0.2Ignoring cookies completely when setting headers turned out to be a mistake due to too many security concerns. Restore them.0.1.3Handle case where user may also be logged in and a cookie not being set.0.1.2Use the s-maxage header for compatability with Varnish.0.1.1Leave response untouched if status code is not 200.0.1Initial release.