Skip to content
Advertisement

memoizee – Cached based on arguments

I would like to memoize results of function based on provided arguments. For example:

getFiles('/articles/en')
getFiles('/articles/pl')

This invocations should be independent and should have standalone cached results. Currently I always receive the result from the first call.

I tried used other library than memoizee. With fast-memoize I got expected effect, but fast-memoize doesn’t allow set maxAge of cached results.

// Services to fetch files
const memoize = require('memoizee')

async function getFile (id) {/*...*/}
async function getFiles (folder) {/*...*/}

const getFilesWithCache =  memoize(getFiles, { maxAge: 86400000, promise: true })
const getFileWithCache =  memoize(getFile, { maxAge: 86400000, promise: true })

module.exports = {
 getFile,
 getFiles
 getFilesWithCache,
 getFileWithCache
}

//First call
let files = await getFilesWithCache('articles/en')


//Second call
files = await getFilesWithCache('articles/pl')

In second call the result is the same as in the first call.

Advertisement

Answer

I sorted out this problem with promise-memoize library.

Advertisement