Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Service Restangular Caching

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.

like image 415
VSO Avatar asked Aug 05 '15 03:08

VSO


People also ask

How does angular use cached data?

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.

How do I bypass angular service worker?

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.

What is cache busting angular?

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.


1 Answers

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 :

  1. 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});
    
  2. You can cache response for single requests like :

    function getSomeThings(){
        Restangular.one('thing', 123).withHttpConfig({ cache: true}).get().then(function (thing) {
            return thing;
        });
    }
    
  3. You can involve a custom $cacheFactory instance to expire or invalidate cached responses when necessary by invoking this : $cacheFactory.get('$http').removeAll()

  4. 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;
    
        }])
    
like image 146
Salem Ouerdani Avatar answered Oct 14 '22 05:10

Salem Ouerdani