Javascript 在检查api请求是否在缓存中后,从本地存储获取该请求的缓存响应

Javascript 在检查api请求是否在缓存中后,从本地存储获取该请求的缓存响应,javascript,caching,local-storage,Javascript,Caching,Local Storage,我正在开发一个应用程序,我必须缓存一些api请求的响应。所以,我使用散列url作为缓存键,并将其存储到本地存储器中。 现在,我的工具应该涵盖这个场景 第一次缓存请求url的响应,并基于缓存密钥存储到本地存储器中 下一次,它将检查响应是否已缓存,如果未缓存,则将缓存并存储 如果它是缓存的,那么缓存的响应将基于“localStorage.getItem('cached\u response')从本地存储为该请求提供服务 在我的应用程序中,存在requestInterceptorhandler和

我正在开发一个应用程序,我必须缓存一些api请求的响应。所以,我使用散列url作为缓存键,并将其存储到本地存储器中。 现在,我的工具应该涵盖这个场景

  • 第一次缓存请求url的响应,并基于缓存密钥存储到本地存储器中

  • 下一次,它将检查响应是否已缓存,如果未缓存,则将缓存并存储

  • 如果它是缓存的,那么缓存的响应将基于“localStorage.getItem('cached\u response')从本地存储为该请求提供服务

  • 在我的应用程序中,存在requestInterceptorhandler和responseInterceptorhandler函数。那么,我如何将下面的cachingFetch方法与特定的请求结合起来呢

  • 因此,到目前为止,我已经尝试实现这一点,但不确定

    缓存代码段

        const hashstr = s => {
      let hash = 0;
      if (s.length == 0) return hash;
      for (let i = 0; i < s.length; i++) {
        let char = s.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
        hash = hash & hash; // Convert to 32bit integer
      }
      return hash;
    }
    
    const cachedFetch = (url, options) => {
      let expiry = 2 * 60 
      if (typeof options === 'number') {
        expiry = options
        options = undefined
      } else if (typeof options === 'object') {
        expiry = options.seconds || expiry
      }
      // Use the URL as the cache key to localStorage
      let cacheKey = hashstr(url)
      let cached = localStorage.getItem(cacheKey)
      let whenCached = localStorage.getItem(cacheKey + ':ts')
      if (cached !== null && whenCached !== null) {
        // Even though 'whenCached' is a string, this operation
        // works because the minus sign tries to convert the
        // string to an integer and it will work.
        let age = (Date.now() - whenCached) / 1000
        if (age < expiry) {
          let response = new Response(cached)
          return Promise.resolve(response)
        } else {
          // We need to clean up this old key
          localStorage.removeItem(cacheKey)
          localStorage.removeItem(cacheKey + ':ts')
        }
      }
    
      return fetch(url, options).then(response => {
        // let's only store in cache if the content-type is 
        // JSON
        if (response.status === 200) {
          let ct = response.headers.get('Content-Type')
          if (ct && (ct.match(/application\/json/i)) {
            // There is a .json() instead of .text()
            // If we don't clone the response, it will be 
            // consumed by the time it's returned. This 
            // way we're being un-intrusive. 
            response.clone().text().then(content => {
              localStorage.setItem(cacheKey, content)
              localStorage.setItem(cacheKey + ':ts', Date.now())
            })
          }
        }
        return response
      })
    }
    
    const hashstr=s=>{
    设hash=0;
    如果(s.length==0)返回哈希;
    for(设i=0;i{
    //让我们仅在内容类型为的情况下存储在缓存中
    //JSON
    如果(response.status==200){
    让ct=response.headers.get('Content-Type')
    if(ct&(ct.match(/application\/json/i)){
    //有一个.json()而不是.text()
    //如果我们不克隆响应,它将是
    //在它返回时被消耗掉。这
    //我们不干涉别人的方式。
    response.clone().text().then(内容=>{
    setItem(缓存键、内容)
    localStorage.setItem(cacheKey+':ts',Date.now())
    })
    }
    }
    返回响应
    })
    }
    
    如何截取请求并返回缓存的响应?请建议我解决方法