Redis memoization library with good Typescript generics, locking, and argument-wide bulk invalidation
  • TypeScript 100%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2022-10-21 12:50:23 -04:00
.github ci: add release workflow (#34) 2022-10-21 11:22:33 -05:00
.vscode Initial commit 2020-01-17 18:28:42 -05:00
benchmark feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
src feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
.editorconfig Initial commit 2020-01-17 18:28:42 -05:00
.eslintignore feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
.eslintrc feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
.gitignore Initial commit 2020-01-17 18:28:42 -05:00
.npmignore Initial commit 2020-01-17 18:28:42 -05:00
.prettierignore Initial commit 2020-01-17 18:28:42 -05:00
.prettierrc feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
CHANGELOG.md chore(release): 2.0.0 2022-10-21 12:50:23 -04:00
jest.config.ts feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
LICENSE Initial commit 2020-01-17 18:27:52 -05:00
package.json chore(release): 2.0.0 2022-10-21 12:50:23 -04:00
README.md docs: update package name and homepage 2020-09-07 10:45:31 -04:00
tsconfig.json feat: done, all tests passing 2020-01-17 18:28:42 -05:00
tsconfig.module.json feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00
yarn.lock feat: update to redis v4 cli (#33) 2022-10-21 11:22:46 -05:00

memoredis

Redis memoization library with good Typescript generics, locking, and argument-wide bulk invalidation

Usage

yarn add memoredis

Initialization

import memoredis from 'memoredis';

// pass options as they would directly to redis.createClient
const memoizer = memoredis({ clientOpts: 'redis://' });

// or pass in an existing client
const client = redis.createClient();
const memoizer = memoredis({ client });

Basic Usage

const expensiveDatabaseLookup = async ({ authorId, genre }) => {
  // some expensive database lookup
  return { countOfBooks: n };
};

const countOfBooksPublishedByAuthorInGenre = memoizer.memoize(
  expensiveDatabaseLookup,
  { key: 'genre-author-count', ttl: 60 * 1000 }
);

await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // goes to the database
await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // served from redis!

Basic Invalidation

await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // goes to the database
await memoizer.invalidate('genre-author-count', {
  authorId: 4,
  genre: 'fiction'
});
await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // goes to the database

Bulk Partial Invalidation

await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // goes to the database
await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'biology' }); // goes to the database
await memoizer.invalidate('genre-author-count', {
  authorId: 4
});
await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'fiction' }); // goes to the database
await countOfBooksPublishedByAuthorInGenre({ authorId: 4, genre: 'biology' }); // goes to the database

Prefixes

// optionally include a prefix for namespacing caches
const libraryA = memoredis({ prefix: 'a' });
const libraryB = memoredis({ prefix: 'b' });

const expensiveDatabaseLookup = async ({ authorId, genre }) => {
  // some expensive database lookup
  console.log('Finished looking up');
  return { countOfBooks: n };
};

const countOfBooksPublishedByAuthorInGenreInLibraryA = libraryA.memoize(
  expensiveDatabaseLookup,
  { key: 'genre-author-count', ttl: 60 * 1000 }
);

const countOfBooksPublishedByAuthorInGenreInLibraryB = libraryA.memoize(
  expensiveDatabaseLookup,
  { key: 'genre-author-count', ttl: 60 * 1000 }
);

await countOfBooksPublishedByAuthorInGenreInLibraryA({
  authorId: 4,
  genre: 'fiction'
}); // goes to the database

await countOfBooksPublishedByAuthorInGenreInLibraryB({
  authorId: 4,
  genre: 'fiction'
}); // goes to the database

Locking

This library uses redis-lock under the hood to prevent two calls to the same function at the same time, even when the cache is cold.

const expensiveDatabaseLookup = async ({ authorId, genre }) => {
  // some expensive database lookup
  console.log('Finished looking up');
  return { countOfBooks: n };
};

const countOfBooksPublishedByAuthorInGenre = memoizer.memoize(
  expensiveDatabaseLookup,
  { key: 'genre-author-count', ttl: 60 * 1000 }
);

const args = { authorId: 4, genre: 'fiction' };
await Promise.all([
  countOfBooksPublishedByAuthorInGenre(args),
  countOfBooksPublishedByAuthorInGenre(args)
]);
// only logs 'Finished looking up' once