caches.match和cache.match

时间:2018-05-12 12:20:30

标签: javascript web-services caching

我们在服务工作者中使用caches.match(event.request)来执行"仅缓存策略"。我注意到我们也在cache.match('someURL')承诺之后立即返回caches.open("cache-name")。这很令人困惑。

caches.match(event.request)cache.match('someURL')之间有什么区别?每个用例的用例是什么?

示例案例:

Caches.match

self.addEventListener('fetch', function(event) {
  event.respondWith(caches.match(event.request)); 
});

Cache.match

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.open('mysite-dynamic').then(function(cache) {
      return cache.match(event.request).then(function (response) {
        return response || fetch(event.request).then(function(response) {
          cache.put(event.request, response.clone());
          return response;
        });
      });
    })
  );
});

2 个答案:

答案 0 :(得分:2)

我猜你并不知道" caches"参考CacheStorage 基本上缓存或缓存存储存储所有缓存,而缓存只是缓存存储中的命名缓存。基本上caches.match应该为您提供缓存存储的实例,而cache.match则为您提供特定缓存的实例。

答案 1 :(得分:1)

cache.match特定的缓存中搜索项目,而caches.match all 的缓存中搜索匹配项。