我已经使用缓存数据构建了一个Web App。但需要每隔一小时删除缓存数据

时间:2017-04-13 07:23:53

标签: javascript angular caching

我已经使用缓存数据构建了一个Web App。

如果页面第二次加载而不进行任何网络调用,则提供缓存数据

但后端的数据每隔一小时刷新一次。

我想在没有setInterval

的情况下每隔一小时忽略缓存或删除缓存



private fireAnalyticsGETAPI(methodName, paramString?, serveFromCache?):Observable<any> {

    let pString = paramString ? paramString : AnalyticsDataService.getBaseParamsURL();
    let url = this.getAPIEndpoint() + methodName + encodeURI(pString);

    if(serveFromCache===undefined){
      serveFromCache = true
    }

    //getting the data from cache store
    let cData = this.phenomCacheService.getData(url);

 
    if(serveFromCache===false){
      cData = null;
    }
   // if cache is present serve cache to UI
    if(cData){
      return Observable.create(observer =>
              {
                setTimeout(()=>{
                  return observer.next(cData);
                },10);

              });

    }else{ 
     //If cache is not present fire API to get data serve to UI and store in CACHE
      console.log("Firing API with url :: " + url);
      let authHeaders = this.loginService.getAuthorizationHeaders();
      return this.http.get(url, {
        headers: authHeaders
      }).map((res)=>{
        //setting the data in cache store to use it further cases
        let data = this.extractData(res);
        this.phenomCacheService.setData(url,data);
        return data;
      }).catch((e)=> {
            return this.handleError(e);
          });

    }
  }
&#13;
&#13;
&#13;

0 个答案:

没有答案