JoethaCoder JoethaCoder - 1 year ago 108
AngularJS Question

Angular Proper way to cache or load http requests

This is really more of a question. I am using several http requests in my application that spans an entire site really.

  1. Is it better to cache the request or is there a more efficient manner to only make the request on given URL's?

  2. Am I understanding the cache method correctly (see below code)

homeApp.controller('mainMenu', function($scope, $http) {
$http.get("http://localhost:3000/wp-json/menus/2", {
cache: true
}).then(function(response) {
$ =;

I am trying to keep this thing as quick and agile as possible. Thanks all!

Answer Source
  1. You could probably customise the HTTP provider to look for certain paths and cache them for your application. But I think coding the specific calls you want cached by specifying cache=true is better and clearer?

  2. I think you have understood it correctly.$http

"When caching is enabled, $http stores the response from the server using the relevant cache object. The next time the same request is made, the response is returned from the cache without sending a request to the server."

So if you repeatedly make requests to /wp-json/menus/2 within the scope of an instance of your angular SPA, it will not hit the server. Its quite efficient.

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download