$http cache in Angularjs and processing of multiple $HTTP requests

Source: Internet
Author: User

In Angularjs's actual project, it is often necessary to process multiple $http requests, each $HTTP request returns a promise, and we can put multiple promise into an array argument accepted by the $q.all () method.

Processing multiple $HTTP Requests

Angular.module (' app ', []). Controller (' Appctrl ',functionAppctrl (myservice) {varApp = This; Myservice.getall (). Then (function(info) {App.myinfo=info; }). Service (' MyService ',functionMyService ($http, $q) {varMyService = This; User= ' Https://api ... ', Repos= ' ', Events= ' '; Myservice.getdata=functionGetData () {return$http. Get (user). Then (function(userData) {return{name:userData.data.name, Url:userData.data.url, Repocount:                UserData.data.count})}; Myservice.getuserrepos=functionGetuserrepos () {return$http. Get (Repos). Then (function(response) {return_.map (Response.data,function(item) {return{name:item.name, description:item.description, Starts:item.startCount}})} Myservice.getusereve NTS=functiongetuserevents () {...} Myservice.getall=function(){            varUserpromise =myservice.getdata (), Usereventspromise=Myservice.getuserrepos (), Userrepospromise=Myservice.getuserrepos (); return$q. All ([Userpromise, Usereventspromise, Userrepospromise]). Then (function(){                ....            }) }    })

$http Request Caching

The $http get method The second parameter accepts an object that has a cache field that can accept a bool type to implement caching, or {cache:true}, or a service can be accepted.

Create a service by factory and inject the service into the controller.

Angular.module (' app ', []). Factory ("Mycache",function($cacheFactory) {return$cacheFactory ("Me"); }). controller ("Appctrl",function($http, Mycache) {varApp = This; App.load=function() {$http. Get ("Apiurl", {Cache:mycache}). Success (function(data) {App.data=data; })} App.clearcache=function() {Mycache.remove ("Apiurl"); }    })

Above

In fact, implementing the caching mechanism is $cachefactory
Placing the caching mechanism in the current request via {Cache:mycache}
$cacheFactory the request API as a key, so when the cache is clear, it is also based on this key to clear the cache

$http cache in Angularjs and processing of multiple $HTTP requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.