Disable Caching on FeatureLayer.refresh()

6966
7
04-09-2012 06:12 AM
JamesCornwell-Shiel
New Contributor
Is there a way to disable caching on an OnDemand mode FeatureLayer? When I use the .refresh() method it refreshes, but more often than not the actual data returned isn't different, and I know that the layer has been updated. Zooming updates the data at the new zoom level, but on returning to the original scale the features are back to what was originally loaded.

I've tried manually updating the url of the FeatureLayer with a timestamped querystring via two methods:

var newtimestamp = new Date().getTime();
layer.url = layer.url.replace(/nocache=.*/,'nocache='+newtimestamp);
layer.refresh();


and

var newtimestamp = new Date().getTime();
layer._url.query.nocache = newtimestamp;
layer.refresh();


Neither actually changes the URL hit by the layer.refresh() though. Any suggestion on what method/variable to use to do this, or a different way to get around this caching issue?

Thanks in advance!
0 Kudos
7 Replies
JamesCornwell-Shiel
New Contributor
As a brief update, I have somewhat resolved this by setting the web server's CacheControl header to NO-CACHE. I'll have to filter this down to just feature services, but that should be fine. That said, there may be times when I don't have control over the server, so the question still stands as to whether there's a JS-side way to handle the issue of service caching.
0 Kudos
demdeberanz
New Contributor II
I'm interested in this topic too. It's very important to disable the caching when you, for example, change dynamically the definition expression.

Please someone reply!xD

Thank you everyBody
0 Kudos
by Anonymous User
Not applicable
I've also noticed this behavior when using Safari (and thus all iOS devices) where calling refresh() on a FeatureLayer does not actually refresh the features on the client. Examining the request shows that the querystring contains neither a timestamp (_ts) nor a dojo.preventCache parameter.

My solution is to modify the request with a preventCache option before it is sent to the server. I set this up in my initialization method.
esri.setRequestPreCallback(function (ioArgs) {    
    if (ioArgs.url.indexOf("FeatureServer") > -1 &&
        ioArgs.url.indexOf("returnGeometry=true") > -1) {
        ioArgs.preventCache = true;
    }
    return ioArgs;
});


Interestingly, the featurelayer in the Silverlight API has an option to disable client caching. Might be nice to see that capability ported to the JSAPI.
Hope this helps.
ThomasDickerson
New Contributor
I agree, setting "ioArgs.preventCache = true" in a RequestPreCallback function as suggested above seems to have resolved an issue I was having in IE8.  Thanks!
0 Kudos
DimitarKolev
New Contributor II
Seems that the problem persists in IE9.
It works fine in Firefox18.

Can any one can verify that in IE9?
Please share if you have a solution to disable caching in IE9?

Thanks
0 Kudos
DimitarKolev
New Contributor II
my bad:rolleyes:.
I did not pay attention that the
ioArgs.preventCache = true;
was never executed because ioArgs.url did not have indexOf("returnGeometry=true").
I dont know if this is a change in ArcGIS 10.1 or else.
After examining the properties of ioArgs in the Firefox debugger this is what I came up with:
ioArgs.content.returnGeometry == true

            //modify the request with a preventCache option before it is sent to the server
            // so the feature layer is not cached in the browser
            esri.setRequestPreCallback(function (ioArgs) {
                try {
                    if (ioArgs.url.indexOf("FeatureServer") > -1 &&
                    ioArgs.content.returnGeometry == true) {
                        ioArgs.preventCache = true;
                    }
                    return ioArgs;

                } catch (e) {
                    console.log(e.toString());
                    return ioArgs;
                }               
            });


Worked in IE9 and Firefox18.
Mindaugas_ižas
New Contributor
I had this problem for a very long time, but it looks that managed to find the solutions.
It is only if you use proxy page.
Just add
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
to your proxy.jsp and all the requests will not be cached.
I am using JSAPI 2.8 and server 10.0

Hope this helps
0 Kudos