[AngualrJS] Using Angular-Cache for caching http request

Check the website: https://jmdobry.github.io/angular-cache/#using-angular-cache-with-http

 

Install:

npm install --save angular-cache

 

  getServiceDetail( serviceId = "mock" ) {

    if(!this.CacheFactory.get(`${this.prefix}.${serviceId}`)){
        this.$http.defaults.cache = this.CacheFactory(`${this.prefix}.${serviceId}`, {
            maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
            cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
            deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
        });
    }

    return this.$http.get( `${this.CONFIG.BACKEND_API_URL}/services/${serviceId}/details`,
    {
        cache: this.CacheFactory.get(`${this.prefix}.${serviceId}`)
    } )
               .then( ( res ) => {

                   return res.data.serviceDetail;
               }, ( err ) => {
                 this.$log.debug( `ServiceDetails --> getServiceDetail err: Cannnot get detail data: ${JSON.stringify( err, null, 3 )}` );
                 throw err;
               } );
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值