Read more

Angular: Caching API responses in Restangular

Thomas Klemm
November 14, 2014Software engineer

Restangular can make use of $http's built-in response cache.

# Cache response for single request
Restangular.one('accounts', 123).withHttpConfig({ cache: true }).get();

# Cache responses for all requests (be careful with that, you might work with stale data)
RestangularProvider.setDefaultHttpFields({ cache: true });
Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

To invalidate cached responses e.g. on a state change in UI Router, you can do

@app.run ['$rootScope', '$cacheFactory', ($rootScope, $cacheFactory) ->
  $rootScope.$on '$stateChangeSuccess', ->
    $cacheFactory.get('$http').removeAll()
]

Alternative approaches might involve a custom $cacheFactory instance for your Restangular resources where you could expire cached responses whenever nescessary.

Posted by Thomas Klemm to makandra dev (2014-11-14 11:35)