Sharing Top Content from the Angular-sphere.

Caching HTTP requests in AngularJS

  • Caching HTTP requests in AngularJS
  • { ‘get’ : { method : ‘GET’ , cache : true } } ) ;
  • AngularJS made me stop hiding from JavaScript
  • As you can see, having access to the cache object allows us to choose whatever method we want to use the cache.
  • In the post I will show two different approaches to caching resources: the built-in angular way using $resource and a home-grown solution.

In AngularJS, especially when you’re using a ‘modern’ Web Component like approach, you often have directives request the same information from your services multiple times. Since we’d rather not do round-trips we don’t need to to save on server resources caching is our go-to solution. In this post I will show two different approaches to caching resources: the built-in angular way using $resource and a home-grown solution.

@jdriven_nl: Caching HTTP requests in AngularJS #AngularJS #Coding #FrontEnd

In AngularJS, especially when you’re using a ‘modern’ Web Component like approach, you often have directives request the same information from your services multiple times. Since we’d rather not do round-trips we don’t need to to save on server resources caching is our go-to solution. In this post I will show two different approaches to caching resources: the built-in angular way using $resource and a home-grown solution.

Our first example uses angular-resource. It’s the most straightforward way of accessing REST resources and enabling caching is simply a matter of setting the cache to true

Then there is also the manual approach. A $http.get returns a reusable promise that can easily be stored in an object. A manual implementation gives full control over cache size and allows you to use whatever eviction policy you prefer, but the code is less concise than simply using $resource and $cacheFactory:

As you can see, having access to the cache object allows us to choose whatever method we want to use the cache. The downside is that we have to write quite a bit more code

so it might become tempting to reinvent the wheel to create our own managed cache. If the objective is to simply save on $http round-trips I would strongly suggest just using

Caching HTTP requests in AngularJS

Comments are closed, but trackbacks and pingbacks are open.