Skip to content

Instantly share code, notes, and snippets.

@CreaturePhil
Last active January 17, 2017 22:19
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 CreaturePhil/911961ef30b7ca98a1ee53641bb760fd to your computer and use it in GitHub Desktop.
Save CreaturePhil/911961ef30b7ca98a1ee53641bb760fd to your computer and use it in GitHub Desktop.
temp cache db
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