Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Created November 12, 2015 04:04
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 cgmartin/2df6f68d08388bc8c398 to your computer and use it in GitHub Desktop.
Save cgmartin/2df6f68d08388bc8c398 to your computer and use it in GitHub Desktop.
node-cache-manager promisify example (w/ bluebird)
'use strict';
var express = require('express');
var usersApi = require('../lib/users-api');
var Promise = require('bluebird');
var cacheManager = require('cache-manager');
var cache = cacheManager.caching({store: 'memory', max: 100, ttl: 900});
Promise.promisifyAll(cache);
var router = module.exports = express.Router();
router.get('/:user_id/reviews', getUserReviews);
/**
* Get a user's latest reviews
*/
function getUserReviews(req, res, next) {
// Pull previous result from cache (if found)
var userId = req.params['user_id'];
var cacheKey = 'userReviews_' + userId;
cache
.wrapAsync(cacheKey, function cacheMiss(cacheCb) {
usersApi
.getMemberReviews(userId) // returns promise
.asCallback(cacheCb);
})
.then(function(reviews) {
res.json(reviews);
})
.catch(function(err) {
return next(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment