A function memoizer using memcache-client.
Install:
npm i memcache-client-memoizer
memoizer(options)
options
:object
. Required. An object with the following keys:client
:memcache-client instance
. Required. A memcache-client instance.fn
:Function
. Required. The function to memoize, must return a Promise.keyFn
:(args to fn) => 'key-string'
. Required. A function which returns a string cache-key for memcached. 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}`
setOptions
:object
. Optional.memcached-client
command options.
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 client = new MemcacheClient({ server: 'localhost:11211' })
const fnToMemoize = ({ name, color }) => Promise.resolve({ name, color })
const memoizedFn = memoizer({
client,
fn: fnToMemoize,
keyFn: ({ name, color }) => `${name}:${color}`
})
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'}