memcache-client-memoizer

A function memoizer using a get/set cache client.

Install:
npm i memcache-client-memoizer
API
memoizer(options)
Arguments
options
: object
. Required. An object with the following keys:
Note:
Rejected promises are not memoized - since that's probably not what you want :)
const MemcacheClient = require('memcache-client')
const { memoizer } = require('memcache-client-memoizer')
const fnToMemoize = ({ name, color }) => Promise.resolve({ name, color })
const memoizedFn = memoizer({
clientProviderFn: () => new MemcacheClient({ server: 'localhost:11211' }),
fn: fnToMemoize,
keyFn: ({ name, color }) => `${name}:${color}`,
cacheResultTransformFn: ({value}) => value,
skipCacheFn: ({ name, color }) => false,
})
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... })
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... })
const Catbox = require('catbox');
const Memory = require('catbox-memory');
const cacheTtlMilliseconds = 1000 * 60 * 5;
const client = new Catbox.Client(Memory);
await client.start();
const fnToMemoize = ({ name, color }) => Promise.resolve({ name, color })
const memoizedFn = memoizer({
client,
fn: fnToMemoize,
keyFn: ({ name, color }) => ({ segment: 'test', id: 'test-cache' }),
setOptions: cacheTtlMilliseconds,
cacheResultTransformFn: ({ item }) => item,
skipCacheFn: ({ name, color }) => false,
})
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... })
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... })