我在angular js中有一个$http服务,它启用了缓存。第一次加载时,应用程序服务得到调用并获得缓存。现在,当我在同一页面的任何地方调用服务时,数据来自缓存,但当我更改页面路由并再次从另一个页面调用服务时数据来自服务器(我只是更改路由而不是刷新页面)
编辑=>代码正在工作!!在路由上,数据也来自缓存,但它需要更多的时间,因为几乎没有其他调用。它只需要更多的钱,然后我接受缓存来响应。如果我从任何点击事件调用相同的数据,那么它将需要2ms到3ms
这是我的服务
commonServicesModule.service('getDetails', ['$http', 'urlc',
function($http, urlc) {
return {
consumer: {
profile: function(id) {
return $http({
url: urlc.details.consumer.profile,
cache: true,
params: {
'consumer_id': id,
'hello': id,
},
method: 'GET',
}).success(function(result) {
return result;
});
},
}
}
}
])
来自控制器的呼叫:
start = new Date().getTime();
/*get user information */
getDetails.consumer.profile('1').then(function(results) {
console.log('time taken for request form listCtrl ' + (new Date().getTime() - start) + 'ms');
});
当我在路线后从其他地方打电话时,需要同样的时间。
尝试将consumer
对象移动到函数体中,并返回对它的引用,如下所示:
commonServicesModule.service('getDetails', ['$http', 'urlc', function($http, urlc) {
var getConsumer = {
profile: function(id) {
return $http({
url: urlc.details.consumer.profile,
cache: true,
params: {
'consumer_id': id,
'hello': id,
},
method: 'GET',
}).success(function(result) {
return result;
});
}
};
return { consumer: getConsumer };
}]);