npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

redis-json-memoize

v1.0.1

Published

Caches the returned value of promises in the provided redis cache

Readme

redis-json-memoize

Caches the JSON response of a provided promise into the provided Redis. Ideal for caching responses from database calls in your API.

If you are looking to reap the benefits of Redis caching in your API, this library provides a simple and easy-to-use solution.

Compatible with both ioredis and node redis.

For more info on Redis: https://redis.io/topics/introduction

Install

npm install --save redis-json-memoize

Usage Examples

Providing Redis instance directly

const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

module.exports = redisMemoize.memoize(getUserByName, { redis: redisInstance });

Initializing module with Redis instance

const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

redisMemoize.initialize({
	redis: redisInstance,
});

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

module.exports = redisMemoize.memoize(getUserByName);

Clearing cached responses for a specific function

const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

redisMemoize.initialize({
	redis: redisInstance,
});

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

const cachedGetUserByName = redisMemoize.memoize(getUserByName);

module.exports = {
	getUser: cachedGetUserByName,
	clearCachedUsers: cachedGetUserByName.clearCache, // Will clear cached responses for this function only. Can accept a locator string.
}

API

memoize (fn : Function, options: Object)

Returns a memoized version of the provided function.

  • fn Provided function

    • Must return a promise
    • Should be named as specifically as possible (fn.name will be used to write the redis key for the cached response)
    • NOTE: it is not recommended that you use this module to memoize Express/Hapi request handlers. Redis keys are written using a combination of fn.name and the function inputs stringified. HTTP requests are large circular objects, so the memoization will not be effective
    • fn.clearCache memoizedFunction.clearCache will clear Redis keys that were written memoizedFunction only. Similar to global clearCache method, can accept a locator string.
  • options

    • redis This is where the Redis instance is provided. Compatible with both ioredis and node-redis.
    • ttl Sets the TTL (time to loss) for responses memoized from this function. NOTE: Redis TTL's are in seconds, not milliseconds.

initialize (options: Object)

Initializes implementation of redis-json-memoize.

  • redis Object. Required. A redis instance.
  • ttl Number. TTL for cached data in your Redis. If not provided, a default is used.
  • disableLogs Boolean. Defaults to false. If true, all logs from redis-json-memoize will be muted.
  • prefetchRatio Number. Defaults to 0.7. Determines the time, as a ratio of TTL, at which cached data is marked as "stale". When Redis returns a "stale" object, it triggers a background refresh of that data.
  • startupTime Number. Default 5 seconds. Determines how much time is allowed at startup for Redis to boot before the module begins memoizing provided functions.
  • promiseLibrary Object. Default is native Promise. This gives you an option to provide Bluebird or another promise library if you feel the desire to do so.

clearCache (locator: String, redis: Object)

Clears cached responses from Redis.

  • locator String. Not required. If provided, only Redis keys that contain this string will be cleared
  • redis Object. Not Required if initialize has been used. A redis instance.

TODOS

  • Test coverage
  • Improve documentation surrounding clearing of cache