[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;
               } );
  }
}
原文地址:https://www.cnblogs.com/Answer1215/p/5239138.html