--- title: WindowOrWorkerGlobalScope.caches slug: Web/API/WindowOrWorkerGlobalScope/caches translation_of: 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.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 40.0 {{CompatGeckoDesktop(42)}}
{{CompatGeckoDesktop(52)}}[1]
{{CompatNo}} {{CompatUnknown}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile(42)}}
{{CompatGeckoMobile(52)}}[1]
{{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

[1] caches 现在被定义在 {{domxref("WindowOrWorkerGlobalScope")}} 中的mixin里.

相关链接