--- title: WindowOrWorkerGlobalScope.caches slug: Web/API/caches translation_of: Web/API/WindowOrWorkerGlobalScope/caches original_slug: Web/API/WindowOrWorkerGlobalScope/caches ---
{{APIRef()}}{{SeeCompatTable}}
caches 是{{domxref("WindowOrWorkerGlobalScope")}}接口的一个只读属性,它返回了与当前上下文紧密相关的{{domxref("CacheStorage")}}对象。此对象激活了诸如存储用于离线使用的资产,并生成对请求生成自定义响应等功能。
var myCacheStorage = self.caches; // or just caches
{{domxref("CacheStorage")}} 对象.
下面的例子展示了你在service worker上下文中如何应该运用cache对离线资产的进行存储。
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/',
'/sw-test/gallery/bountyHunters.jpg',
'/sw-test/gallery/myLittleVader.jpg',
'/sw-test/gallery/snowTroopers.jpg'
);
})
);
});
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('Service Workers', '#self-caches', 'caches')}} | {{Spec2('Service Workers')}} | Defined in a WindowOrWorkerGlobalScope partial in the newest spec. |
| {{SpecName('Service Workers')}} | {{Spec2('Service Workers')}} | Initial definition. |