Use cache_control decorator instead of cache_page

I used cache_page originally as it sets all the relevant HTTP headers
("Cache-Control" and "Expires") to ask browsers/proxys to cache the
generated image to reduce load.

What I didn't know at the time is that cache_page also inserts the
generated images into the cache backend. Sites with many images being
served thorugh the "Frontend URLs" mechanism could end up with their
cache servers being overloaded.

This pull request replaces the use of cache_page with cache_control
which doesn't insert the image into the cache.

Also, cache_control only sets the "Cache-Control" HTTP header (and not
"Expires"). As "Cache-Control" is defined in the HTTP 1.1 spec, I think
it's safe to assume that the vast majority of browsers and proxys
support it.
pull/652/head
Karl Hobley 2014-09-26 10:44:44 +01:00
rodzic 944fd8897f
commit e292f87e53
2 zmienionych plików z 3 dodań i 5 usunięć

Wyświetl plik

@ -783,9 +783,6 @@ class TestFrontendServeView(TestCase):
self.assertEqual(response['Cache-Control'].split('=')[0], 'max-age')
self.assertTrue(int(response['Cache-Control'].split('=')[1]) > datetime.timedelta(days=30).seconds)
self.assertIn('Expires', response)
self.assertTrue(dateutil.parser.parse(response['Expires']) > timezone.now() + datetime.timedelta(days=30))
def test_get_invalid_signature(self):
"""
Test that an invalid signature returns a 403 response

Wyświetl plik

@ -1,13 +1,14 @@
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from django.core.exceptions import PermissionDenied
from django.views.decorators.cache import cache_page
from wagtail.wagtailimages.models import get_image_model, Filter
from wagtail.wagtailimages.utils.crypto import verify_signature
from django.views.decorators.cache import cache_control
@cache_page(60 * 60 * 24 * 60) # Cache for 60 days
@cache_control(max_age=60*60*24*60) # Cache for 60 days
def serve(request, signature, image_id, filter_spec):
image = get_object_or_404(get_image_model(), id=image_id)