Skip to content

Instantly share code, notes, and snippets.

@alexkahn
Created February 9, 2015 14:44
Show Gist options
  • Save alexkahn/ef161a4a534d45490764 to your computer and use it in GitHub Desktop.
Save alexkahn/ef161a4a534d45490764 to your computer and use it in GitHub Desktop.
Memjs + Mongoose = Easy caching of counts
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;
});
@alexkahn
Copy link
Author

alexkahn commented Feb 9, 2015

Where this will live yet, I'm not entirely sure but I think it is the simplest thing to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment