Last active
October 4, 2018 11:08
-
-
Save biswanaths/b8999c9e4e5a9e979dcde061074528f6 to your computer and use it in GitHub Desktop.
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 localdb = require('node-persist') | |
const when = require('when'); | |
class QueuedStorage { | |
constructor() { | |
this.storage = localdb.create({ ttl: true, logging: false}) | |
} | |
async init() { | |
await this.storage.init(); | |
} | |
async getItem(key) { | |
this.current = when(this.current, | |
() => { return this.storage.getItem(key)}, | |
() => { return this.storage.getItem(key)}); | |
return this.current; | |
} | |
async setItem(key, value) { | |
this.current = when(this.current, | |
() => { return this.storage.setItem(key,value)}, | |
() => { return this.storage.setItem(key,value)}); | |
return this.current; | |
} | |
}; | |
//const storage = new QueuedStorage(); | |
const storage = localdb.create({ ttl: true, logging: true }) | |
async function main () { | |
await storage.init() | |
try { | |
const key = 'DSF-AS-558-DDDF'; | |
const fs = new Array(10000) | |
.fill(0) | |
.map((e,i) => function() { | |
if(i%2) return storage.getItem(key) | |
else return storage.setItem(key,i)}()); | |
Promise.all(fs).then(values => console.log(values)); | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
main().catch(err => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment