Search code examples
djangodjango-cachegoogle-cloud-cdn

How to add "public" by default to cached views by Django?


Im trying to use Google Cloud CDN to cache Django responses. Django properly sets the cache-control header with the max age, but it is missing the 'public' part of the header.

Basically, currently all cached views have:

cache-control: max-age=3600

But I want:

cache-control: max-age=3600,public

Edit: My current settings.py have the following for caching:

MIDDLEWARE = [
    ...
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    ...
]

....

CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_SECONDS = 3600
CACHE_MIDDLEWARE_KEY_PREFIX = ''
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.environ.get('CACHE_URL'),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "IGNORE_EXCEPTIONS": True,
            "MAX_ENTRIES": 10000,
        }
    }
}

So I basically followed https://docs.djangoproject.com/en/3.1/topics/cache/. So I cached the entire site. And for those views which I don't want cached I use @never_cache.

Edit 2: I could add "public=True" to each view with an annotation but I want a solution for the entire site.


Solution

  • For now I ended up just going for a simple middleware. Though I am open for suggestions.

    class PublicCacheMiddleware:
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            response = self.get_response(request)
    
            if response.has_header('Cache-Control') and 'private' not in response.get('Cache-Control'):
                response['Cache-Control'] += ',public'
    
            return response