litter-box
A few function memoization helpers to work with catbox.
Promise Memoization
litterBox.memoizeFnPromise(options)
Arguments
options
:object
. Required. An object with the following keys:client
:Catbox Client Instance
. Required. A catbox client instance.fn
:Function
. Required. A function which returns a Promise.ttl
:Integer
. Required. The time-to-live for the cachedfn
result.keyProvider
:(fn-input) => {id, segment}
. Required. A function which returns a cache-key for Catbox. This function is called with the same arguments asfn
, allowing you to create a dynamic cache-key, for example:
const exampleKeyProvider = segment: 'test' id: `test-`
Promise Memoization Example:
This code is also available here.
const litterBox = const examplePromiseFunction = Promise const cachedPromiseFunction = litterBox // prints 1234 // later on... // function not executed, value is pulled from the cache
Callback Memoization
litterBox.memoizeFnCallback(options)
Arguments
options
:object
. Required. An object with the following keys:client
:Catbox Client Instance
. Required. A catbox client instance.fn
:Function
. Required. A function which has a callback as it's final argument.ttl
:Integer
. Required. The time-to-live for the cachedfn
result.keyProvider
:(fn-input) => {id, segment}
. Required. A function which returns a cache-key for Catbox. This function is called with the same arguments asfn
, allowing you to create a dynamic cache-key, for example:
const exampleKeyProvider = segment: 'test' id: `test-`
Callback Memoization Example:
This code is also available here.
const litterBox = const exampleCallbackFunction = const cachedCallbackFunction = litterBox // prints 1234 // later on... // function not executed, value is pulled from the cache // prints 1234
l