MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

CacheStorage 缓存存储

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

The CacheStorage interface represents the storage for Cache objects. It provides a master directory of all the named caches that a ServiceWorker, other type of worker or window scope can access (you don't have to use it with service workers, even though that is the spec that defines it) and maintains a mapping of string names to corresponding Cache objects.

CacheStorage also exposes CacheStorage.open() and CacheStorage.match(). Use CacheStorage.open() to obtain a Cache instance. Use CacheStorage.match() to check if a given Request is a key in any of the Cache objects that the CacheStorage object tracks.

You can access CacheStorage through the global caches property.

Note: CacheStorage always rejects with a SecurityError on untrusted origins (i.e. those that aren't using HTTPS, although this definition will likely become more complex in the future.) When testing you can get around this by checking the "Enable Service Workers over HTTP (when toolbox is open)" option in the Firefox Devtools options/gear menu.
Note: CacheStorage.match() is a convenience method. Equivalent functionality to match a cache entry can be implemented by opening your cache with CacheStorage.open(), returning the entries it contains with CacheStorage.keys(), and matching the one you want with CacheStorage.match().

方法

CacheStorage.match()
Checks if a given Request is a key in any of the Cache objects that the CacheStorage object tracks and returns a Promise that resolves to that match.
CacheStorage.has()
Returns a Promise that resolves to true if a Cache object matching the cacheName exists.
CacheStorage.open()
Returns a Promise that resolves to the Cache object matching the cacheName (a new cache is created if it doesn't exist.)
CacheStorage.delete()
Finds the Cache object matching the cacheName, and if found, deletes the Cache object and returns a Promise that resolves to true. If no Cache object is found, it returns false.
CacheStorage.keys()
Returns a Promise that will resolve with an array containing strings corresponding to all of the named Cache objects tracked by the CacheStorage. Use this method to iterate over a list of all the Cache objects.

举例

This code snippet is from the MDN sw-test example (see sw-test running live.) This service worker script waits for an InstallEvent to fire, then runs waitUntil to handle the install process for the app. This consists of calling CacheStorage.open to create a new cache, then using Cache.addAll to add a series of assets to it.

In the second code block, we wait for a FetchEvent to fire. We construct a custom response like so:

  1. Check whether a match for the request is found in the CacheStorage. If so, serve that.
  2. If not, fetch the request from the network, then also open the cache created in the first block and add a clone of the request to it using Cache.put (cache.put(event.request, response.clone()).)
  3. If this fails (e.g. because the network is down), return a fallback response.

Finally, return whatever the custom response ended up being equal to, using FetchEvent.respondWith.

this.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open('v1').then(function(cache) {
      return cache.addAll([
        '/sw-test/',
        '/sw-test/index.html',
        '/sw-test/style.css',
        '/sw-test/app.js',
        '/sw-test/image-list.js',
        '/sw-test/star-wars-logo.jpg',
        '/sw-test/gallery/bountyHunters.jpg',
        '/sw-test/gallery/myLittleVader.jpg',
        '/sw-test/gallery/snowTroopers.jpg'
      ]);
    })
  );
});

this.addEventListener('fetch', function(event) {
  var response;
  event.respondWith(caches.match(event.request).catch(function() {
    return fetch(event.request);
  }).then(function(r) {
    response = r;
    caches.open('v1').then(function(cache) {
      cache.put(event.request, response);
    });
    return response.clone();
  }).catch(function() {
    return caches.match('/sw-test/gallery/myLittleVader.jpg');
  }));
});

说明

Specification Status Comment
Service Workers
CacheStorage
Working Draft Initial definition.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 40.0 44 (44)[1] 未实现 ? 未实现
Accessible from Window 43.0 44 (44)[1] ? ? ?
Accessible from WorkerGlobalScope 43.0 44 (44)[1] ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 未实现 44.0 (44) (Yes) (Yes) (Yes) 40.0
Accessible from Window 未实现 未实现 44.0 (44) ? ? ? 43.0
Accessible from WorkerGlobalScope 未实现 未实现 44.0 (44) ? ? ? 43.0

[1] Service workers (and Push) have been disabled in the Firefox 45 Extended Support Release (ESR.)

查看其他

文档标签和贡献者

标签: 
 此页面的贡献者: fedwatch
 最后编辑者: fedwatch,