【发布时间】:2018-07-20 07:03:06
【问题描述】:
我想创建一个带有 etags 和角度拦截器的缓存解决方案。
一切正常,但我无法返回缓存的数据。
我想用缓存的数据覆盖304 Not Modified 响应。
'responseError': function(rejection) {
if(rejection.status === 304){
var response = {};
var url = rejection.config.url;
var params = rejection.config.params || {};
var etagKey = url+'.'+JSON.stringify(params);
var storedValue = $localStorage['ETag-Cache'+etagKey] || '{}';
var cachedObj = JSON.parse(storedValue);
console.log('CACHED ETag-Cache'+etagKey,cachedObj.response);
//I'd like to return with the cached data here but this doesn't work
return cachedObj.response;
}
return $q.reject(rejection);
}
下面的示例请求
$http.get('/api/bigdata', {
}).then(function(resp){
//I'd like to get the cached data in the resp variable
});
【问题讨论】:
-
304 响应由浏览器自动处理。不会传递给 $http 服务。
-
我在 $http.then(resp) 中得到了某种响应(承诺),所以理论上它可以工作:) 如果我用 $http.cache(resp) 捕获 304 响应) 我可以处理这个,但我不想在整个项目的每个文件中添加每个请求,在拦截器中处理这个会很棒。
标签: angularjs caching angular-http-interceptors etags