Skip to content

Instantly share code, notes, and snippets.

@akinjide

akinjide/cache.js

Created Aug 23, 2018
Embed
What would you like to do?
let cache = {}
const complex_computation = (a, b, timeout=5000, callback) => {
setTimeout(() => {
callback(null, a + b)
}, timeout)
}
const cached_computation = (a, b, callback) => {
const key = a + ':' + b
if (cache[key]) {
return callback(null, cache[key])
}
complex_computation(a, b, 10000, (err, data) => {
cache[key] = data
callback(err, data)
})
}
// EXEC
console.time("QueryTime")
cached_computation(2, 3, (err, data) => {
if (err != null) {
console.log(error)
return console.timeEnd("QueryTime")
}
console.log(data)
console.timeEnd("QueryTime")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.