pngtile/tile.py
changeset 172 73380dd6a816
parent 171 0ff809f855f7
child 173 e7822b0be417
--- a/pngtile/tile.py	Sat Oct 04 01:40:54 2014 +0300
+++ b/pngtile/tile.py	Sat Oct 04 02:19:43 2014 +0300
@@ -9,6 +9,8 @@
 import pngtile.application
 import pypngtile
 
+import datetime
+
 ## Coordinates
 # width of a tile
 TILE_SIZE = 256
@@ -44,6 +46,9 @@
     return scale(val, zoom) - dim / 2
 
 class TileApplication (pngtile.application.PNGTileApplication):
+    # age in seconds for caching a known-mtime image
+    MAX_AGE = 7 * 24 * 60 * 60 # 1 week
+
     def __init__ (self, image_server, **opts):
         """
             image_server:       http://.../ url to image-server frontend
@@ -153,6 +158,14 @@
         # http caching
         mtime = image.cache_mtime()
 
+        if 't' in request.args:
+            try:
+                ttime = datetime.datetime.utcfromtimestamp(int(request.args['t']))
+            except ValueError:
+                ttime = None
+        else:
+            ttime = None
+
         if request.if_modified_since and mtime == request.if_modified_since:
             return Response(status=304)
             
@@ -163,6 +176,18 @@
         response = Response(png, content_type='image/png')
         response.last_modified = mtime
 
+        if not ttime:
+            # cached item may change while url stays the same
+            response.headers['Cache-Control'] = 'must-revalidate'
+
+        elif ttime == mtime:
+            # url will change if content changes
+            response.headers['Cache-Control'] = 'max-age={max_age:d}'.format(max_age=self.MAX_AGE)
+
+        else:
+            # XXX: mismatch wtf
+            response.headers['Cache-Control'] = 'must-revalidate'
+
         return response