javascript - 我怎么知道 $http 响应是否来自缓存 - Angular

标签 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) }) //alerts true

如您所见,第一个调用实际上是在执行 ajax 调用,因此它不是来自缓存,第二个调用从缓存中获取数据,因此它会发出 true

最佳答案

我不确定信息是否公开 - 文档提到即使资源被缓存,请求(使用缓存)仍然是异步的,因此它看起来像任何其他请求。我唯一能想到的就是滚动你自己的缓存并使用它来知道何时发生了成功的查找。来自文档:

You can change the default cache to a new object (built with $cacheFactory) by updating the $http.defaults.cache property. All requests who set their cache property to true will now use this cache object.

关于javascript - 我怎么知道 $http 响应是否来自缓存 - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814621/

相关文章:

javascript - 如何使用 JavaScript 将 <td> 标签添加到我的 html 表格中?

javascript - 在 Codeigniter 中使用模态 Bootstrap 作为删除提示

ruby-on-rails - 如何防止 Rails 中的浏览器页面缓存

java - 如何以编程方式清除 Windows 中的缓存?

java - 为什么 Guava 的 Cache.invalidate(Object key) 方法不是通用的?

javascript - 无法从网络服务获取响应

javascript - 如何从 tinymce textarea 中删除标题属性

python - 为什么我可以接收数据但无法正确渲染数据(带有flask-restapi/jinja2的angularjs)

javascript - 使用 ui-router 切换状态时保留 $scope

javascript - Kendoui TreeView 展开清除选中的节点