A NodeJS package that makes it easy to cache objects and files locally and in AWS
npm install @makerx/node-cache
The primary purpose of this package is to make it easy to cache objects and files.
import { S3 } from '@aws-sdk/client-s3'
import { FileSystemObjectCache, S3ObjectCache } from '@makerx/node-cache'
const cache =
process.env.CACHE_BUCKET_NAME === 'local'
? new FileSystemObjectCache(path.join(__dirname, '../.cache'))
: new S3ObjectCache(
new S3({
region: process.env.AWS_REGION,
}),
process.env.CACHE_BUCKET_NAME,
'optional/cache/prefix',
)
for (let i =0; i < 10; i++>) {
const value = await cache.getAndCache('test', async (existing: {test: number} | undefined) => {
await new Promise(resolve => setTimeout(resolve, 100))
const e = existing ?? {test: 1}
e.test++
return e
}, {
staleAfterSeconds: 1
})
console.log(value)
await new Promise(resolve => setTimeout(resolve, 500))
}
await cache.put('test', {test: 100})