Skip to content
/ re-mem Public

🧠 Memoization with stale-while-revalidate and stale-if-error

License

Notifications You must be signed in to change notification settings

fabsrc/re-mem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

re-mem

Fork of mem with "staleWhileRevalidate" and "staleIfError"

Node.js CI

Install

npm install -S @fabsrc/re-mem

Usage

import reMem from "@fabsrc/re-mem"

function getData(id) {
  return Promise.resolve(`Data: ${id}`)
}
const getDataMemoized = reMem(getData, {
  maxAge: 1000,
  staleWhileRevalidate: 10000,
  staleIfError: 20000
})

getDataMemoized(123)
  .then(console.log)
  .catch(console.error)

Options

  • cacheKey Function that returns a cache key based on arguments passed to a function. By default the first argument is used as cache key
  • cache Custom cache to store data in. (Default: new Map())
  • cachePromiseRejection Boolean flag wether to cache rejected Promises or not (Default: false)
  • maxAge Time in ms to return the cached promise (Default: Infinity)
  • staleWhileRevalidate Time in ms to return stale data while revalidating the data in the background. The time starts after maxAge runs out.
  • staleIfError Time in ms to return stale data if original promise rejects with an error.

Development

Testing

npm test