Last active
January 17, 2017 22:19
-
-
Save CreaturePhil/911961ef30b7ca98a1ee53641bb760fd to your computer and use it in GitHub Desktop.
temp cache db
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 steno = require('steno') | |
const fs = require('graceful-fs') | |
let temp = {} | |
function write(key, value) { | |
temp[key] = value; | |
fs.readFile('test.json', 'utf-8', (err, data) => { | |
console.log(data) | |
if (!data) data = '{}'; | |
const o = Object.assign(JSON.parse(data), temp); | |
steno.writeFile('test.json', JSON.stringify(o), (err) => { | |
delete temp[key]; | |
}); | |
}); | |
} | |
function read(key) { | |
if (temp[key]) { | |
return new Promise((res, rej) => { | |
res(temp[key]) | |
}) | |
} else { | |
return new Promise((res, rej) => { | |
fs.readFile('test.json', 'utf-8', (err, data) => { | |
if (!data) data = '{}'; | |
const o = JSON.parse(data); | |
res(o[key]) | |
}); | |
}) | |
} | |
} | |
write('mackzsad', 1123) | |
read('hoes').then(s => console.log(1, s)) | |
read('mackzsad').then(s => console.log(2, s)) | |
read('kalsjd').then(s => console.log(3, s)) | |
write('alksjd', 1123) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment