import { Injectable } from '@angular/core' ; import { HttpRequest , HttpResponse } from '@angular/common/http' ; @ Injectable ( { providedIn : 'root' } ) export class RequestCacheService { /**
     * Max time the cache entry is valid in ms.
     */ public readonly maxAge = 2000 ; private private cache : Map < any , any > = new Map < any , any > ( ) ; param req public get ( req ) { const url = req . urlWithParams ; const cached = this . cache . get ( url ) ; if ( ! cached ) { return undefined ; } const isExpired = cached . lastRead < ( Date . now ( ) - this . maxAge ) ; return isExpired ? undefined : cached . response ; } param req param response public put ( req , response ) { const url = req . urlWithParams ; const entry = { url , response , lastRead : Date . now ( ) } ; this . cache . set ( url , entry ) ; const expired = Date . now ( ) - this . maxAge ; this . cache . forEach ( expiredEntry => { if ( expiredEntry . lastRead < expired ) { this . cache . delete ( expiredEntry . url ) ; } } ) ; } }