Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using OpenLayers (GeoExt) with google chrome browser. All WMS layer is for live database. I have refreshed the browser at certain interval using javascript but the data is cached in browser and when I refresh web page I see old data. When I clear cache from browser I see new data. How do I prevent caching or clear the cache automatically?

function RefreshPage(Period) {
    setTimeout("location.reload(true);", Period);
}
share|improve this question

2 Answers 2

up vote 5 down vote accepted

If I remember correctly, you can insert your own parameter into the WMS layer configuration:

var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
    "http://wms.jpl.nasa.gov/wms.cgi",
    {
        layers: "modis,global_mosaic",
        transparent: true,
        myData: Math.random()
    }, {
        opacity: 0.5,
        singleTile: true
    });

You can use the current time or another random value. The key myData will be URL encoded into the request sent to the WMS provider and the request is now changed due to the new value of myData.

This doesn't clear the cache, but it tells the browser that the request is different from the one it previously cached.

If you just want to refresh your WMS layers, this can be done by

myWMSLayer.redraw(true);

The parameter true tells the layer to force a redraw.

share|improve this answer

The trick of using the "myData" parameter should be unnessesarry, as we see in the OpenLayers.Layer.HTTPRequest (which the WMS layer inherits from):

redraw: function(force) { 
    if (force) {
        return this.mergeNewParams({"_olSalt": Math.random()});
    } else {
        return OpenLayers.Layer.prototype.redraw.apply(this, []);
    }
},

if you do layer.redraw(true); OpenLayers adds a random parameter for you

(besides, setting myData to Math.random() on initialization will not change myData for each update)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.