Last active
August 29, 2015 13:57
-
-
Save strathmeyer/9556850 to your computer and use it in GitHub Desktop.
Demo hapi's caching of method results
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
var Hapi = require('hapi'); | |
var server = new Hapi.Server('localhost', 8000); | |
var add = function (a, b, next) { | |
console.log("Calculating..."); | |
next(null, a + b); | |
}; | |
server.method('sum', add, { cache: { expiresIn: 2000 } }); | |
server.start(function() { | |
server.methods.sum(4, 5, function (err, result) { | |
console.log(result); | |
// Force serial execution | |
server.methods.sum(4, 5, function (err, result) { | |
// Doesn't log "Calculating" | |
console.log(result); | |
}); | |
}); | |
// Run later | |
setTimeout(function() { | |
// Does not log "Calculating" | |
server.methods.sum(4, 5, function (err, result) { | |
console.log(result); | |
}); | |
}, 1000); | |
// Run after cache expires | |
setTimeout(function() { | |
// Logs "Calculating" | |
server.methods.sum(4, 5, function (err, result) { | |
console.log(result); | |
}); | |
}, 3000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment