javascript/node.js memoisation library

Nikhil Ranjan
2 min readApr 26, 2019

--

Many times in node.js we want to cache a async function result to improve performance or to avoid rate limiting from underlying APIs. Caching follows a general approach:

  • setup a cache, in memory or in some store like redis
  • create a key from arguments of the function
  • check if key exist in cache, return result if exists
  • call async function if not exist
  • save/memoise result in cache
  • return result

This often result in some bootstrap code for setting up cache etc. To avoid this code duplication i have created a library for memoisation of async functions in node.js. You can install it from npm:

npm install memoise

memoise is a memoisation/caching module for node.js. Results are cached in redis or memory, via a backing store. Redis is used if REDIS_HOST, REDIS_PORT and REDIS_DB environment variables are set otherwise it fallback to in memory lru store.

Usage: create a cache with max 10000 items(only for lru) and a TTL of 300 seconds

const Memoise = require('memoise')
const memoiser = new Memoise({ max: 10000, maxAge: 300 })

Then wrap your original async function like this

const cached = memoiser.wrap(original)

Now call the wrapper as you would call the original function

await cached(arg1, arg2,...argn)

memoiser.wrap wraps a given function and returns a cached version of it. Functions to be wrapped must be async function and not object methods. Sometimes, you may want to use a different value of this inside the caller function.

The arguments are used to create the key. Subsequently, when the wrapped function is called with the same n arguments, it would lookup the key in LRU, and if found, call the callback with the associated data. It is expected that the callback will never modified the returned data, as any modifications of the original will change the object in cache.

memoiser.debug can be used to check cache efficiency and stats.

--

--