Javascript 如何找到服务工作者响应所属的缓存?

Javascript 如何找到服务工作者响应所属的缓存?,javascript,service-worker,cachestorage,Javascript,Service Worker,Cachestorage,给定一个请求,我如何确定它属于哪个缓存? match,尽管在所有缓存中搜索,但只会给我缓存的响应。我想检索缓存对象/缓存的名称和缓存的响应。如果您需要知道响应所在的缓存,恐怕需要分别调用每个缓存上的match,直到找到匹配的缓存为止 代码可能如下所示: async function findCachedResponse(request) { const cacheKeys = await caches.keys(); const openCaches = await Promis

给定一个请求,我如何确定它属于哪个缓存?
match
,尽管在所有缓存中搜索,但只会给我缓存的响应。我想检索缓存对象/缓存的名称和缓存的响应。

如果您需要知道响应所在的缓存,恐怕需要分别调用每个缓存上的
match
,直到找到匹配的缓存为止

代码可能如下所示:

async function findCachedResponse(request) {
    const cacheKeys = await caches.keys();
    const openCaches = await Promise.all(cacheKeys.map(cacheKey => caches.open(cacheKey)));
    const matches = await Promise.all(openCaches.map(cache => cache.match(request));
    const i = matches.findIndex(match => match !== undefined);
    if (i !== -1) {
        return { cache: cacheKeys[i], response: matches[i] };
    } else {
        return undefined;
    }
}