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.
Using shareReplay shareReplay helps us cache data in our apps quickly and replay the data to new subscribers. Add the shareReplay operator in the flow of data, take the data from the HTTP request, and put it into a buffer so that it can replay the last emission of my HTTP request.
To bypass the service worker, set ngsw-bypass as a request header, or as a query parameter. The value of the header or query parameter is ignored and can be empty or omitted.
To prevent visitors from being served with an older, cached version of the file, which might lack some translation, you can employ a cache-busting functionality. This entails attaching a unique version identifier to the file, which ensures that a fresh version is fetched, instead of a stale, cached version.
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;
}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With