Skip to content
This repository was archived by the owner on Feb 22, 2026. It is now read-only.

Repository files navigation

node-cache

Simple in-memory caching for Node.js apps.

Getting Started

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(): void

Example usage

import{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}`)}}

Releases

Used by

Contributors

Languages