--- title: CacheStorage slug: Web/API/CacheStorage tags: - API - CacheStorage - ServiceWorker translation_of: Web/API/CacheStorage --- <p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p> <p><strong><code>CacheStorage</code></strong> 接口表示 {{domxref("Cache")}} 对象的存储。它提供了一个 {{domxref("ServiceWorker")}} 、其它类型worker或者 {{domxref("window")}} 范围内可以访问到的所有命名cache的主目录(它并不是一定要和service workers一起使用,即使它是在service workers规范中定义的),并维护一份字符串名称到相应 {{domxref("Cache")}} 对象的映射。</p> <p><code>CacheStorage</code> 同样暴露了 {{domxref("CacheStorage.open()")}} 和 {{domxref("CacheStorage.match()")}}方法。使用 {{domxref("CacheStorage.open()")}} 获取 {{domxref("Cache")}} 实例。使用 {{domxref("CacheStorage.match()")}} 检查给定的 {{domxref("Request")}} 是否是 <code>CacheStorage</code> 对象跟踪的任何 {{domxref("Cache")}} 对象中的键。</p> <p>你可以通过 {{domxref("WorkerGlobalScope.caches", "caches")}} 属性访问 <code>CacheStorage</code> .</p> <div class="note"><strong>注意</strong>: CacheStorage 总是对不受信任的源(即那些不使用HTTPS,尽管此定义将来很可能变得更加复杂。)使用 <code>SecurityError</code> reject. 测试时,你可以在Firefox Devtools选项/齿轮菜单中通过选中"通过HTTP启用 Service Workers (当工具箱打开时)" 选项来绕开这个限制。</div> <div class="note"><strong>注意</strong>: {{domxref("CacheStorage.match()")}} 是一个便捷方法。匹配cache条目的同等功能可以通过执行 {{domxref("CacheStorage.open()")}} 打开cache,使用 {{domxref("CacheStorage.keys()")}} 返回它包含的条目,并将你所需的条目与 {{domxref("CacheStorage.match()")}} 匹配。</div> <h2 id="方法">方法</h2> <dl> <dt>{{domxref("CacheStorage.match()")}}</dt> <dd>检查给定的 {{domxref("Request")}} 是否是 {{domxref("CacheStorage")}} 对象跟踪的任何 {{domxref("Cache")}} 对象的键,并返回一个resolve为该匹配的 {{jsxref("Promise")}} .</dd> <dt>{{domxref("CacheStorage.has()")}}</dt> <dd>如果存在与 <code>cacheName</code> 匹配的 {{domxref("Cache")}} 对象,则返回一个resolve为true的 {{jsxref("Promise")}} .</dd> <dt>{{domxref("CacheStorage.open()")}}</dt> <dd>返回一个 {{jsxref("Promise")}} ,resolve为匹配 <code>cacheName</code> (如果不存在则创建一个新的cache)的 {{domxref("Cache")}} 对象</dd> <dt>{{domxref("CacheStorage.delete()")}}</dt> <dd>查找匹配 <code>cacheName</code> 的 {{domxref("Cache")}} 对象,如果找到,则删除 {{domxref("Cache")}} 对象并返回一个resolve为true的 {{jsxref("Promise")}} 。如果没有找到 {{domxref("Cache")}} 对象,则返回 <code>false</code>.</dd> <dt>{{domxref("CacheStorage.keys()")}}</dt> <dd>返回一个 {{jsxref("Promise")}} ,它将使用一个包含与 {{domxref("CacheStorage")}} 追踪的所有命名 {{domxref("Cache")}} 对象对应字符串的数组来resolve. 使用该方法迭代所有 {{domxref("Cache")}} 对象的列表。</dd> </dl> <h2 id="示例">示例</h2> <p>此代码片段来自于MDN <a href="https://github.com/mdn/sw-test/">sw-test example</a> (请参阅<a href="https://mdn.github.io/sw-test/">sw-test running live</a>.)此 service worker 脚本等待 {{domxref("InstallEvent")}} 触发,然后运行 {{domxref("ExtendableEvent.waitUntil","waitUntil")}} 来处理应用程序的安装过程。这包括调用 {{domxref("CacheStorage.open")}} 创建一个新的cache,然后使用 {{domxref("Cache.addAll")}} 向其添加一系列资源。</p> <p>在第二个代码块,我们等待 {{domxref("FetchEvent")}} 触发。我们构建自定义相应,像这样:</p> <ol> <li>检查CacheStorage中是否找到了匹配请求的内容。如果是,使用匹配内容。</li> <li>如果没有,从网络获取请求,然后同样打开第一个代码块中创建的cache,并使用 {{domxref("Cache.put")}} (<code>cache.put(event.request, response.clone())</code>.) 将请求的clone副本添加到它。</li> <li>如果此操作失败(例如,因为网络关闭),返回备用相应。</li> </ol> <p>最后,使用 {{domxref("FetchEvent.respondWith")}} 返回自定义响应最终等于的内容。</p> <pre class="brush: js">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'); })); });</pre> <h2 id="说明">说明</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}</td> <td>{{Spec2('Service Workers')}}</td> <td>Initial definition.</td> </tr> </tbody> </table> <h2 id="浏览器兼容">浏览器兼容</h2> <p>{{Compat("api.CacheStorage")}}</p> <h2 id="参见">参见</h2> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> <li>{{domxref("Cache")}}</li> <li>{{domxref("WorkerGlobalScope.caches")}}</li> </ul>