Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能知道$http响应是否来自缓存_Javascript_Angularjs_Caching - Fatal编程技术网

Javascript 我怎样才能知道$http响应是否来自缓存

Javascript 我怎样才能知道$http响应是否来自缓存,javascript,angularjs,caching,Javascript,Angularjs,Caching,$http是否有任何迹象表明响应来自缓存? 例如,假设我打了两个电话: $http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts false $http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) })

$http
是否有任何迹象表明响应来自缓存?
例如,假设我打了两个电话:

$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts false 

$http.get({url, cache: true).then(function(response, ..., moreData) { alert(moreData.fromCache) }) //alerts true

正如您所看到的,第一个调用实际上是在执行一个ajax调用,因此它不是来自缓存,第二个调用从缓存中带来数据,因此它会发出true警报,我不确定信息是否已公开-文档中提到,即使资源已缓存,请求(使用缓存)仍将是异步的,所以它看起来就像其他任何请求一样。我能想到的唯一一件事就是滚动您自己的缓存,并使用它来知道何时成功进行了查找。从文件中:

可以将默认缓存更改为新对象(使用 $cacheFactory)更新$http.defaults.cache属性。全部的 将其缓存属性设置为true的请求现在将使用此缓存 反对


通过查看网络选项卡,可以查看调用是否通过Chrome开发者工具缓存。它将告诉您它是被缓存还是被调用。

很容易确定是否使用带有警告的请求和响应处理程序缓存了某些内容。此
仅处理第一个请求在发出第二个请求之前完成的情况。如果您在第一个请求返回之前发出2个
请求,angular cache将像一个符咒一样处理该请求,并且仅发出1个服务调用,则两者都将显示为未缓存,这在技术上是正确的,因为它们都在等待第一个请求被缓存,但可能不是您想要的

$httpProvider.interceptors.push(function() {
    return {
        'request': function requestInterceptor(config) {
            if (config.cache) {
                //Determine if a custom cache object is being used
                var cache = angular.isObject(config.cache) ? config.cache : $http.defaults.cache;
                config.wasCached = cache.get(config.url) ? true : false;
            }

            return config;
        },

        'response': function(response) {
            if (response && response.config && response.config.wasCached) {
                console.log('Was Cached!')
            } 
            return response;
        }
    };
});

我希望仍然有一些迹象表明数据是否来自缓存。如果没有这样的迹象,我认为这将是一个非常好的功能,我需要它是编程