Skip to content

Instantly share code, notes, and snippets.

@strathmeyer
Last active August 29, 2015 13:57
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 strathmeyer/9556850 to your computer and use it in GitHub Desktop.
Save strathmeyer/9556850 to your computer and use it in GitHub Desktop.
Demo hapi's caching of method results
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