Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Temporary cache of API requests
const path = require('path')
const cacheManager = require('cache-manager')
// storage for the cachemanager
const fsStore = require('cache-manager-fs')
const fsStoreHash = require('cache-manager-fs-hash');
// initialize caching on disk
const mbOfStorage = 512
const storagePath = path.resolve(__dirname, 'xxx') || '/tmp'
const SECONDS = 60
const MINUTES = 60
const ONE_HOUR = SECONDS * MINUTES
const diskCache = cacheManager.caching({
store: fsStoreHash,
options: {
ttl: ONE_HOUR /* seconds */,
/* max size in bytes on disk */
maxsize: mbOfStorage * 1000 * 1000,
path: storagePath,
// preventfill: true
}
}
);
console.log('diskCache', diskCache)
// diskCache.set('test','lolol', () => {
// console.log('done')
// })
;(async () => {
//await diskCache.set('foo', 'value');
console.log(await diskCache.get('foo')); //"value"
// await diskCache.del('key');
// console.log(await diskCache.get('key')); //undefined
// console.log(await getUserCached(5)); //{id: 5, name: '...'}
// console.log(await getUserCached(5)); //{id: 5, name: '...'}
// //await diskCache.reset();
// function getUserCached(userId) {
// return diskCache.wrap(userId /* cache key */, function () {
// return getUser(userId);
// });
// }
// async function getUser(userId) {
// return {id: userId, name: '...'};
// }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment