--- title: Cache.put() slug: Web/API/Cache/put translation_of: Web/API/Cache/put --- <p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p> <p><span class="seoSummary">{{domxref("Cache")}} 接口的 <strong><code>put()</code></strong> 方法 允许将键/值对添加到当前的 {{domxref("Cache")}} 对象中.</span></p> <p><span class="seoSummary">通常,你只想 </span>{{domxref("GloblFetch.fetch","fetch()")}} 一个或多个请求,然后直接添加结果到cache中。这种情况下,最好使用 {{domxref("Cache.add","Cache.add()")}}/{{domxref("Cache.addAll","Cache.addAll()")}},因为它们是一个或者多个这些操作的便捷方法。</p> <pre class="brush: js">fetch(url).then(function(response) { if (!response.ok) { throw new TypeError('Bad response status'); } return cache.put(url, response); }) </pre> <div class="note"> <p><strong>注意</strong>: <code>put()</code> 将覆盖先前存储在匹配请求的cache中的任何键/值对。</p> </div> <div class="note"> <p><strong>注意</strong>: {{domxref("Cache.add")}}/{{domxref("Cache.addAll")}} 不会缓存 <code>Response.status</code> 值不在200范围内的响应,而 {{domxref("Cache.put")}} 允许你存储任何请求/响应对。因此,{{domxref("Cache.add")}}/{{domxref("Cache.addAll")}} 不能用于不透明的响应,而 {{domxref("Cache.put")}} 可以。</p> </div> <div class="note"> <p><strong>注意</strong>: 当响应主体完全写入磁盘时,初始Cache执行 (在 Blink 和 Gecko中) resolve {{domxref("Cache.add")}}、{{domxref("Cache.addAll")}} 和 {{domxref("Cache.put")}} promise. 更新的规范版本中声明:即使响应主体仍在流式传输,一旦条目被记录到数据库中,浏览器就可以 resolve promise.</p> </div> <h2 id="语法">语法</h2> <pre class="syntaxbox">cache.put(request, response).then(function() { // request/response pair has been added to the cache }); </pre> <h3 id="参数">参数</h3> <dl> <dt>request</dt> <dd>The {{domxref("Request")}} you want to add to the cache.</dd> <dt>response</dt> <dd>The {{domxref("Response")}} you want to match up to the request.</dd> </dl> <h3 id="返回值">返回值</h3> <p>A {{jsxref("Promise")}} that resolves with void.</p> <div class="note"> <p><strong>Note</strong>: The promise will reject with a <code>TypeError</code> if the URL scheme is not <code>http</code> or <code>https</code>.</p> </div> <h2 id="示例" style="line-height: 30px; font-size: 2.14285714285714rem;">示例</h2> <p>This example is from the MDN <a href="https://github.com/mdn/sw-test/">sw-test example</a> (see <a href="https://mdn.github.io/sw-test/">sw-test running live</a>). Here we wait for a {{domxref("FetchEvent")}} to fire. We construct a custom response like so:</p> <ol> <li>Check whether a match for the request is found in the {{domxref("CacheStorage")}} using {{domxref("CacheStorage.match","CacheStorage.match()")}}. If so, serve that.</li> <li>If not, open the <code>v1</code> cache using <code>open()</code>, put the default network request in the cache using {{domxref("Cache.put","Cache.put()")}} and return a clone of the default network request using <code>return response.clone()</code>. Clone is needed because <code>put()</code> consumes the response body.</li> <li>If this fails (e.g., because the network is down), return a fallback response.</li> </ol> <pre class="brush: js">var response; var cachedResponse = 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', 'Cache')}}</td> <td>{{Spec2('Service Workers')}}</td> <td>Initial definition.</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Cache.put")}}</p> <h2 id="See_also">See also</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>