1

I am facing a peculiar issue wrt caching. I am using chrome currently I have angularjs service as below

var url = '/api/acts/' + accountId + '/policy/?filter=NEW_POLICY';
                return $http({
                    method : 'GET',
                    url : url,
                    cache : false,
                    headers: {'disableErrorRedirect': true,'Cache-Control' : 'no-cache'}
                });

Here i have set all attributes of not using the cache and always making a fresh call. But I have seen that 3 out of 5 times it is picking the same value. After sometime it automatically refreshes. Server side there is not caching issue as I am not caching it. In Chrome Dev tools, there is an option called Disable Cache and when i check it , the results are never cached and I get the latest results. I want to make sure that always refereshed data should be return despite of any settings on chrome. I am really confused how to handle this. As mentioned above, I have written all the angularjs and http code to avoid caching. Any help would be appreciated.

Monish Das
  • 383
  • 2
  • 12
  • 28

2 Answers2

4

I had this same issue and resolved it by declaring the cache config globally. It's not ideal, but better than having to add a random string.

angular.module('app', [])
  .config(function ($httpProvider) {
    $httpProvider.defaults.headers.common['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.cache = false;
  });
Seth Moore
  • 106
  • 5
0

What is the file you are trying to get? At the end of your url it has /policy/?filter=NEW_POLICY but there is no actual file name before the question mark and after the slash. But anyway...

You can always ignore the cached version and get from the server by using /baseroot/id/postroot/file?random_string. so for example:

var rand_num = Math.random();
var url = '/api/acts/' + accountId + '/policy/filename?v=' + rand_num + '&filter=NEW_POLICY' 

or just var url = '/api/acts/' + accountId + '/policy/?v=' + rand_num + '&filter=NEW_POLICY' I guess. Like i said, I don't know of situations where you have no filename at the end of the url path.

Not exactly sure if that's a valid query string for the url in your case, but you can try and see if it works.

Sir Neuman
  • 1,385
  • 1
  • 14
  • 23
  • Hi thanks for your reply.. Surely I have thought about this approach to append a random value but that is my last option to fall on. Your solution is valid but i would like to get some angular specific solution where no caching can be allowed at all irrespective of the browser setting – Monish Das Jun 09 '15 at 19:34
  • Ok. I'm not aware of an angular specific way to do that (I wasn't even aware until just looking it up that the angular http object had a way of manipulating the cache). Though if you want to try adding to the list of headers you could try the `pragma` header? Not sure if at all useful but I found it used [here] (http://stackoverflow.com/questions/16098430/angular-ie-caching-issue-for-http) – Sir Neuman Jun 09 '15 at 20:59