import { ICacheEngine } from './types' ; export default class InMemoryCache implements ICacheEngine { private store : { [ key ] : string ; } = { } ; private storeCacheExpiration : { [ key ] : number ; } = { } ; private storeExpirationCheckInMs = 10 ; constructor ( storeExpirationCheckInMs ? ) { if ( storeExpirationCheckInMs ) { this . storeExpirationCheckInMs = storeExpirationCheckInMs ; } this . monitorStoreForExpiredValues ( ) ; } get ( key ) { return this . store [ key ] ; } set ( key , value , cacheTimeInMs = 0 ) { this . store [ key ] = value ; this . storeCacheExpiration [ key ] = new Date ( ) . getTime ( ) + cacheTimeInMs ; } private monitorStoreForExpiredValues ( ) { const { store , storeCacheExpiration } = this ; setInterval ( ( ) : void => { const currentTime = new Date ( ) . getTime ( ) ; Object . keys ( storeCacheExpiration ) . forEach ( ( key ) : void => { if ( storeCacheExpiration [ key ] < currentTime ) { delete storeCacheExpiration [ key ] ; delete store [ key ] ; } } ) ; } , this . storeExpirationCheckInMs ) ; } }