memcache-client-memoizer
v2.2.1
Published
Memoizes promise-returning functions via memcache-client
Downloads
7
Readme
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:client
:{ get: (anything) => Promise, set: (anything, value, options) }
. A cache client instance, must have aget
andset
method. Theget
method must return a promise.clientProviderFn
:() => client
A function which returns aclient
(defined above); (Either aclient
orclientProviderFn
must be passed.)fn
:Function
. Required. The function to memoize, must return a Promise.keyFn
:(args to fn) => anything
. Required. A function which returns a cache-key (can be anything) for caching. This function is called with the same arguments asfn
, allowing you to create a dynamic cache-key, for example:const exampleKeyFn = ({ name, color }) => `${name}:${color}` // can be anything
setOptions
:anything
. Optional. Formemcached-client
this can be command options.cacheResultTransformFn
.(result-from-cache) => transformed-result
. Function to transform cache-result, defaults to(x) => x
. This is useful if your cache service sends along the value in a different form than is returned by yourfn
.skipCacheFn
:(args to fn) => Boolean
. Optional. A function which indicates that the call tofn
should skip the cache.
Note:
Rejected promises are not memoized - since that's probably not what you want :)
memcache-client example:
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}`, // this can return anything
cacheResultTransformFn: ({value}) => value,
skipCacheFn: ({ name, color }) => false,
})
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... }) // cache miss, fill cache, returns {name: 'Max', color: 'blue'}
// later on...
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... }) // cache hit, returns {name: 'Max', color: 'blue'}
catbox example:
const Catbox = require('catbox');
const Memory = require('catbox-memory');
const cacheTtlMilliseconds = 1000 * 60 * 5; // 5 min
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' }), // this can return anything
setOptions: cacheTtlMilliseconds,
cacheResultTransformFn: ({ item }) => item,
skipCacheFn: ({ name, color }) => false,
})
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... }) // cache miss, fill cache, returns {name: 'Max', color: 'blue'}
// later on...
memoizedFn({name: 'Max', color: 'blue'})
.then((result) => { ... }) // cache hit, returns {name: 'Max', color: 'blue'}