Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created January 29, 2021 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/7c8ffa992388404b03399f0cba59ba6c to your computer and use it in GitHub Desktop.
Save DavidWells/7c8ffa992388404b03399f0cba59ba6c to your computer and use it in GitHub Desktop.
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