Angular $http cache study notes

Article reference

http://stackoverflow.com/questions/40018831/http-service-cache-when-the-method-is-post

http://lib.csdn.net/article/angularjs/30992

 

 

need:

The first time an http request is sent, the data of the request is cached. When the request is sent for the second time, the ajax request is not sent, and the local cached data is directly obtained .

 

angularjs $http cache data only for get requests and JSONP

a) Only GET and JSONP requests are cached

 

If you use {cache:true} configuration information directly, the default stored object is $cacheFactory.get("$http")

b) $cacheFactory.get("$http").info() default storage object

 

angular.module('app',[])
	.factory("myCache", function($cacheFactory){
		return $cacheFactory("me");
	})
	.controller("AppCtrl", function($http, myCache){
		var app = this;
		app.load = function(){
			$http.get("apiurl",{cache:myCache})
				.success(function(data){
					app.data = data;
				})
		};
		app.clearCache = function(){
			myCache.remove("apiurl");
		}
	});

 

$http.post() cache data

For security reasons, post requests are often used in the background, but angularjs does not support $http.post() to cache data, you can simulate this method.

code show as below:

angular.module("klwkOmsApp")
    .factory('httpCacheService', ["$cacheFactory",function($cacheFactory){
        return $cacheFactory("http");
    }]);

angular.module("klwkOmsApp")
    .factory('ApiService', ["$window", "$http", "WAP_CONFIG", "$q", "$log","APP_COLORS","httpCacheService",
        function($window, $http, WAP_CONFIG, $q, $log,APP_COLORS,httpCacheService) {

            var _api = WAP_CONFIG;
            var endpoint = _api.host + ':' + _api.port + _api.path ;
            // md5 encrypted Key
            var md5_key = _api.md5Key;

            function postCache (url, data){
                var targetUrl = endpoint + url;
                var deferred = $q.defer();
                var tempPromise;

                if(httpCacheService[url]){
                    deferred.resolve(httpCacheService[url]);
                } else {
                    / / Determine whether the user has passed parameters, if there are parameters, you need to pass parameters
                    if(data != null && data != undefined && data != ""){
                        tempPromise = $http.post(targetUrl,data);
                    }else{
                        tempPromise = $http.post(targetUrl);
                    }
                    tempPromise.success(function(data,header,config,status) {
                        deferred.resolve(data);
                        httpCacheService[url] = data;
                    }).error(function(msg, code) {
                        deferred.reject(msg);
                        $log.error(msg, code);
                    });
                }

                return deferred.promise;
            }

            var result = {
               "postCache" : postCache
            };
            
            return 	result;
        }]);

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326684850&siteId=291194637