Implement decorators to control proxy caching (for the reverse
authorMagnus Hagander <magnus@hagander.net>
Tue, 12 Jan 2010 18:52:32 +0000 (19:52 +0100)
committerMagnus Hagander <magnus@hagander.net>
Tue, 12 Jan 2010 18:52:32 +0000 (19:52 +0100)
proxy), and enable it for the mirror tracking parts

pgweb/downloads/views.py
pgweb/util/decorators.py

index 66b1cddf7ee1c7ec35d18a17240ca1a1a525518b..76b4312b4d2ee6a4652748d7781d3b6dcab77fc7 100644 (file)
@@ -10,7 +10,7 @@ import os
 from datetime import datetime
 import urlparse
 
-from pgweb.util.decorators import ssl_required
+from pgweb.util.decorators import ssl_required, nocache
 from pgweb.util.contexts import NavContext
 from pgweb.util.helpers import simple_form, add_xml_element
 
@@ -114,6 +114,7 @@ def _get_numeric_ip(request):
        except:
                return None
 
+@nocache
 def mirrorselect(request, path):
        try:
                numericip = _get_numeric_ip(request)
@@ -146,6 +147,7 @@ SELECT countrycode FROM iptocountry WHERE %(ip)s BETWEEN startip and endip LIMIT
        newurl = "%s://%s/%s" % (scheme, host, path)
        return HttpResponseRedirect(newurl)
 
+@nocache
 def mirror_redirect(request, mirrorid, protocol, path):
        try:
                mirror = Mirror.objects.get(pk=mirrorid)
@@ -159,6 +161,7 @@ def mirror_redirect(request, mirrorid, protocol, path):
                path,
        )
 
+@nocache
 def mirror_redirect_old(request):
        # Version of redirect that takes parameters in the querystring. This is
        # only used by the stackbuilder.
index 3929354a03af88f4552f8a3a410ae0fdb4981688..9794e4b4fa59b080a3282193b10fe44776cabde2 100644 (file)
@@ -1,4 +1,24 @@
+import datetime
+
 def ssl_required(fn):
        def _require_ssl(request, *_args, **_kwargs):
                return fn(request, *_args, **_kwargs)
        return _require_ssl
+
+def nocache(fn):
+       def _nocache(request, *_args, **_kwargs):
+               resp = fn(request, *_args, **_kwargs)
+               resp['Cache-Control'] = 's-maxage: 0'
+               return resp
+       return _nocache
+
+def cache(days=0, hours=0, minutes=0, seconds=0):
+       "Set the server to cache object a specified time. td must be a timedelta object"
+       def _cache(fn):
+               def __cache(request, *_args, **_kwargs):
+                       resp = fn(request, *_args, **_kwargs)
+                       td = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
+                       resp['Cache-Control'] = 's-maxage: %s' % (td.days*3600*24 + td.seconds)
+                       return resp
+               return __cache
+       return _cache