Created
February 9, 2015 14:44
-
-
Save alexkahn/ef161a4a534d45490764 to your computer and use it in GitHub Desktop.
Memjs + Mongoose = Easy caching of counts
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 cache = require('memjs').Client.create(); | |
var Location = require('models/Location'); | |
var Report = require('models/Report'); | |
cache.get('locationCount', function(err, value) { | |
// memjs doesn't throw an error if the key doesn't exist. | |
// but both err and value will be null. | |
if (err === null && value === null) { | |
Location.count( function(err, count) { | |
// todo: handle errors | |
cache.set('locationCount', count); | |
}); | |
} | |
res.locals.locationCount = value; | |
}); | |
cache.get('reportCount', function(err, value) { | |
// memjs doesn't throw an error if the key doesn't exist. | |
// but both err and value will be null. | |
if (err === null && value === null) { | |
Report.count( function(err, count) { | |
// todo: handle errors | |
cache.set('reportCount', count); | |
}); | |
} | |
res.locals.reportCount = value; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where this will live yet, I'm not entirely sure but I think it is the simplest thing to do.