Skip to content

laphilosophia/in-memory-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

in-memory-cache

Simple node.js in memory cache utility. in-memory-cache currently matches the **Node.js 20.x API.

Install

npm install @laphilosophia/in-memory-cache

Usage

import MemoryCache, { format } from '@laphilosophia/in-memory-cache'


// format(time: string) function takes parameter as string and converts it to  millisecond
const cache = new Cache({ stdTTL: format("5min") })

// or you can use your own time (ms unit)
// const cache = new Cache({ stdTTL: 18_000 })

const $key = 'cache:key'
if (cache.has($key)) {
  const cached = JSON.parse(cache.get($key))
} else {
  const data = await someAsyncFunction()
  cache.put({
    key: $key,
    value: JSON.stringify(data),
    callback: (key, value) => { /* do some stuff */ }
  })

  return data
}

// also, you can listen to these events (native node:events emitter)

cache.on('put', (key: string, value: any) => {})
cache.on('del', (key: string) => {})
cache.on('clear')
cache.on('expire', (key: string, isExpired: boolean) => {})
cache.on('export', (value: string) => {}) // stringified data object
cache.on('import', (size: number) => {})