0

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.

  • 1
    Unfortunately it's not properly documented, but it could be that promises are cached only once they are fulfilled. You might want to report an issue. – Bergi Jun 20 '19 at 12:59

1 Answers1

0

I sorted out this problem with promise-memoize library.