import { TEN_MINUTES_MS } from '../constants' ; import { logger } from '../logger' ; export interface CachedResult < T > { timestamp : number ; result : T ; } export interface ResultCache < T > { getResultAsync : ( args ? : any ) => Promise < CachedResult < T > > ; } export const createResultCache = < T > ( fn , cacheExpiryMs = TEN_MINUTES_MS ) : ResultCache < T > => { const resultCache = { } ; return { getResultAsync : async ( getArgs ? ) : Promise < CachedResult < T > > => { let timestamp = resultCache [ getArgs ] && resultCache [ getArgs ] . timestamp ; let result = resultCache [ getArgs ] && resultCache [ getArgs ] . result ; if ( ! result || ! timestamp || timestamp < Date . now ( ) - cacheExpiryMs ) { try { result = await fn ( getArgs ) ; timestamp = Date . now ( ) ; resultCache [ getArgs ] = { timestamp , result } ; } catch ( e ) { if ( ! result ) { throw e ; } logger . warn ( `Error performing cache miss update: ${ e }` ) ; } } return { timestamp , result } ; } , } ; } ;