Created
January 29, 2021 06:18
-
-
Save DavidWells/7c8ffa992388404b03399f0cba59ba6c to your computer and use it in GitHub Desktop.
Temporary cache of API requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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