type CacheStoreCallback < T > = ( ) => Promise < T > ; type CacheStoreCached < T > = ( id : string , callback : CacheStoreCallback < T > ) => Promise < T > ; export function createCacheStore < T = string > ( limit = 10 ) { const cache = Array ( limit ) ; return { clear ( ) { cache . length = 0 ; } , cachedIndex ( id ) { return cache . findIndex ( ( entry ) => id === entry ?. [ 0 ] ) ; } , touch ( index ) { cache . unshift ( cache . splice ( index , 1 ) [ 0 ] ) ; } , async get ( id , callback ) { let cachedIndex = this . cachedIndex ( id ) ; if ( cachedIndex > - 1 ) { this . touch ( cachedIndex ) ; return cache [ cachedIndex ] [ 1 ] ; } const data = await callback ( ) ; cachedIndex = this . cachedIndex ( id ) ; if ( cachedIndex > - 1 ) { this . touch ( cachedIndex ) ; return cache [ cachedIndex ] [ 1 ] ; } cache . unshift ( [ id , data ] ) ; cache . length = limit ; return data ; } , } ; }