[mapserver-dev] Cache-control in WMS Requests

Paul Ramsey pramsey at cleverelephant.ca
Mon Mar 17 19:04:15 EDT 2008


All,

After doing a workshop with OpenLayers and Mapserver, one of the
things I noticed was that browsers tend to beat on Mapserver more than
they should, because they are not being told that they can cache the
tiles they have retrieved.  As soon as the tiles move out-of-scope,
they are gone, and moving back forces them to re-load.

For many applications, this behavior is way too hard on the server,
particularly since the browser has a perfectly good local cache
directory we could be taking advantage of.

I have (and it only took me 2 hours!) put together a five-line patch
to mapwms.c that allows an optional "http_max_age" parameter in the
WEB metadata, and will set it.  I provide this as an example for
discussion, not as a finished patch.  I need to add the HTTP 1.0
equivalent before it could be applied.

The main question that came up as I spelunked around looking for the
right place to insert this logic was "where!?".  I ended up settling
on the WMS GetMap, because that's where clients like OpenLayers will
tend to exercise it, but there are a number of places in the code
generating stuff that could be flagged with cache-control information.

mode=legend, mode=map, getlegendgraphic, etc etc.

P.

===================================================================
--- mapwms.c	(revision 7459)
+++ mapwms.c	(working copy)
@@ -2196,8 +2196,15 @@
     img = msDrawMap(map, MS_FALSE);
   if (img == NULL)
       return msWMSException(map, nVersion, NULL);
-

+  /* Set the HTTP Cache-control headers if they are defined
+     in the map object */
+  const char *http_max_age;
+  if( (http_max_age = msLookupHashTable(&(map->web.metadata),
"http_max_age")) )
+  {
+    msIO_printf("Cache-control: max-age=%s\n", http_max_age , 10, 10);
+  }
+
   msIO_printf("Content-type: %s%c%c",
               MS_IMAGE_MIME_TYPE(map->outputformat), 10,10);
   if (msSaveImage(map, img, NULL) != MS_SUCCESS)


More information about the mapserver-dev mailing list