import LRU from "lru-cache" ; interface Options < Arguments extends any [ ] , CacheKey > { maxSize : number ; getCacheKey : ( ... args : Arguments ) => CacheKey ; } export default function lruMemoize < Arguments extends any [ ] , ReturnValue , CacheKey > ( fn , options ) { const lru = new LRU < CacheKey , ReturnValue > ( { max : options . maxSize } ) ; return ( ... args ) => { const cacheKey = options . getCacheKey ( ... args ) ; const cachedReturnValue = lru . get ( cacheKey ) ; if ( cachedReturnValue !== undefined ) { return cachedReturnValue ; } const returnValue = fn ( ... args ) ; lru . set ( cacheKey , returnValue ) ; return returnValue ; } ; }