Search code examples
javascriptangularjscaching

How can I know if the $http response is brought from cache - angularjs


is there any indication from $http if the response brought from the cache?
For example lets say I have made this two calls:

$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts false 

$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts true

as you can see the first call is really executing an ajax call so it is not from cache, the second one brings the data from cache so it alerting true


Solution

  • I'm not sure that information is exposed - the documentation mentions that even if the resource is cached, the request (using the cache) will still be asynchronous, so it'll look like any other request. The only thing I can think of is rolling your own cache and using that to know when a successful lookup has occurred. From the documentation:

    You can change the default cache to a new object (built with $cacheFactory) by updating the $http.defaults.cache property. All requests who set their cache property to true will now use this cache object.