Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Cache.put()

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.

The put() method of the Cache interface allows key/value pairs to be added to the current Cache object.

Often, you will just want to fetch() one or more requests, then add the result straight to your cache. In such cases you are better off just using Cache.add/Cache.addAll, as they are shorthand functions for one of more of these operations:

fetch(url).then(function (response) {
  return cache.put(url, response);
})

Note: put() will overwrite any key/value pair previously stored in the cache that matches the request.

Note: Initial Cache implementations (in both Blink and Gecko) resolve Cache.add, Cache.addAll, and Cache.put promises when the response body is fully written to the disk.  More recent spec versions have newer language stating that the browser can resolve the promise as soon as the entry is recorded in the database even if the response body is still streaming in.

Note: As of Chrome 46, the Cache API will only store requests from secure origins, meaning those served over HTTPS.

구문

cache.put(request, response).then(function() {
  // request/response pair has been added to the cache
});

리턴

A promise that resolves with void.

Note: The promise will reject with a TypeError if the URL scheme is not http or https.

파라메터

request
The Request you want to add to the cache.
response
The response you want to match up to the request.

예제

This code snippet is from the MDN sw-test example (see sw-test running live). Here 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 using CacheStorage.match. If so, serve that.
  2. If not, open the v1 cache using open(), put the default network request in the cache using Cache.put and return a clone of the default network request using return response.clone() — necessary because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
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');
});

규약

Specification Status Comment
Service Workers
The definition of 'Cache' in that specification.
Working Draft Initial definition.

지원 가능 브라우저

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40.0 41 (41) Not supported 24 Not supported
Require HTTPS 46.0 ? ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Not supported Not supported 41.0 (41) ? Not supported ? Not supported 40.0
Require HTTPS Not supported Not supported ? ? ? ? ? 46.0

같이 보기

문서 태그 및 공헌자

 이 페이지의 공헌자: minias
 최종 변경: minias,