Simple in-memory caching for Node.js apps.
Create a new instance of the cache, passing in the TTL for its items in seconds:
constcache=newCache(900)The following methods are exposed:
set(key: string,value: T): voidget(key: string): T|nulldelete(key: string): void// Clears all items from the cacheclear(): voidimport{Cache}from'@savla-dev/node-cache'constcache=newCache<unknown>(900)// 15 minutes TTLasyncfunctionfetchWithCache(url: string): Promise<any>{constcachedData=cache.get(url)if(cachedData){console.log('Returning cached data')returncachedData}console.log('Fetching new data')letresponse: Responsetry{response=awaitfetch(url)}catch(e){thrownewError(`Error during fetch: ${e}`)}try{constdata=awaitresponse.json()cache.set(url,data)returndata}catch(e){console.log(`Error during response parsing: ${e}`)}}