[OpenLayers-Dev] Caching layerimages by browsers

Tim Schaub tschaub at opengeo.org
Thu Mar 4 14:06:11 EST 2010


Hey-

Dipl. Inf. Carsten Eider wrote:
> Hi list,
> 
> while examining my WMS-Servers logs i was surpprised by the amount of
> WMS-requests I found within.
> Using Firefox and Firebug i recognized, that the tiles were not cached
> by the browser, i.e.
> each time they came into the viewport of the map they were re-requested!
> 
> Is there a way to use cached images by the browser instead of requesting
> them time and again?
> 

Caching is best controlled by using the appropriate headers in your 
server responses.

If your server responds with an Expires header, we can't force the 
browser to use a cached image (without essentially rewriting our own 
cache) if the Expires date has been reached.

If your server (or gateway cache) uses Etag or Last-Modified headers, 
the browser will try to make requests with If-None-Match or 
If-Modified-Since headers.  If the server responds with 304, Not 
Modified, the browser will pull from its cache.

Looking at the http://openlayers.org/dev/examples/example.html example, 
you can see Etag headers on the static images (west-mini.png etc.).  The 
GetMap responses from openlayers.org contain Expires headers 
corresponding to the request time (so they immediately expire).  The 
GetMap responses from www2.dmsolutions.ca don't contain any cache 
related headers.

When you refresh the page, requests for static images return 304, Not 
Modified, and the browser pulls those from its cache.  Previous 
responses for GetMap images make the browser think those should not be 
cached, so they are requested with Cache-Control: max-age=0.  We can't 
override the headers in this case, so if your server obeys, it will do 
whatever it does to generate new GetMap responses.

So, you can improve the caching situation by having your server (or 
gateway cache) use appropriate headers on responses.  If you configure a 
gateway cache like Varnish or Squid in front of your WMS, this should be 
handled for you.  If you use a tile generator to pre-seed a tile cache 
and serve those statically, you can configure your server to use 
Last-Modified headers.  If you are confident that you won't be updating 
static images, use future Expires headers to avoid the intermediate 
request with If-None-Match or If-Modified-Since headers.

The short story is that for image requests and effectively using the 
browser's cache, OpenLayers isn't the place to make changes.

Oh, and re-reading your question, I see you were talking about 
panning/zooming instead of reloading the page.  The same advice applies, 
with the additional note that your browser cache can eventually fill up. 
  I think the default configuration for Firefox is 50MB.

Hope that is some help.
Tim

> THX
> Carsten
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Dev mailing list