繁体   English   中英

使用请求名称将数据保存在缓存中

[英]Save data in cache with name of the request

如果我指示请求的名称以这种方式保存,则在获取和更新动态缓存时与服务工作者一起工作: / sites / MLM / search但不保存答案,因为它不留下任何数据。

这是API: https//api.mercadolibre.com/sites/MLM/search?nickname = JBL.MX

这是提取:

 resFetch =  apiFetch( CACHE_DYNAMIC_NAME, e.request);

调用以更新动态缓存的函数:

function apiFetch( cacheName, request ) {

    return fetch( request ) 
        .then( res => {
            if( res.ok ) { 
                updateDynamicCache( cacheName, request, res.clone() ); 
                return res.clone(); 
            } else {
                return cache.match( request );
            }
        })
}

这是更新动态缓存的功能

function updateDynamicCache( dynamicCache, req, response ){

    console.log(req)

    if( response.ok ){ 
        return caches.open( dynamicCache ).then( cache => { 
            cache.put( req, response.clone() );
            return response.clone(); 
        })
    } else {
        return response; 
    }
}

但是,如果不是将请求传递给cache.put,而是传递一个随机名称,它将使用该随机名称和数据缓存键,而不会出现问题:

function updateDynamicCache( dynamicCache, req, response ){

    console.log(req)

    if( response.ok ){ 
        return caches.open( dynamicCache ).then( cache => { 
            cache.put( 'random-name', response.clone() );
            return response.clone(); 
        })
    } else {
        return response; 
    }
}

有人知道如何解决吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM