Are there scenarios where an angular service will cache Restangular/$http calls without being explicitly told to do so? For example I have a service doing something like this:
function getSomeThings(){ return Restangular.one('things').get().then(function (thing) { return thing; }); }
This service gets called every time a page refreshes (it's in the UI-router route resolve). Is there any chance that this call WON'T be made every time, but will be cached by Angular somehow, without explicitly being told to do so?
I am familiar with caching explicitly like so:
RestangularProvider.setDefaultHttpFields({cache: true});
This is NOT the intent. My question is whether angular services have some innate caching logic, and if so, how to override it.
By default Restangular doesn't implement any caching strategies or scenarios, you will need to build your owns. As far as i know, those are what you can do with cache
when working with Restangular :
You can cache everything as you said but you might find yourself working with stale data, so be careful with that :
RestangularProvider.setDefaultHttpFields({cache: true});
You can cache response for single requests like :
function getSomeThings(){ Restangular.one('thing', 123).withHttpConfig({ cache: true}).get().then(function (thing) { return thing; }); }
You can involve a custom $cacheFactory instance to expire or invalidate cached responses when necessary by invoking this : $cacheFactory.get('$http').removeAll()
You can roll in your own cache interface instead of setting true to the cache. This is a factory example that I'm using to remove all cached data whenever I'm sending a create, update or delete request :
.factory('HTTPCache', ['Restangular', '$cacheFactory', function(Restangular, $cacheFactory) { var service = {}; var cache; // Creates the cache service.init = function() { cache = $cacheFactory('http'); Restangular.setDefaultHttpFields({cache: cache}); Restangular.setResponseInterceptor(function(response, operation) { if (operation === 'put' || operation === 'post' || operation === 'remove') { cache.removeAll(); } return response; }) } return service; }])