Skip to content

Instantly share code, notes, and snippets.

@janajri
Last active October 30, 2015 15:05
Show Gist options
  • Save janajri/f7e7d509a7d4c9045073 to your computer and use it in GitHub Desktop.
Save janajri/f7e7d509a7d4c9045073 to your computer and use it in GitHub Desktop.
var redis = require('redis')
var cache = redis.createClient()
var subjectQueryParams = {'_id': 0, 'school_state': 1, 'resource_type': 1, 'poverty_level': 1, 'date_posted': 1, 'total_donations': 1, 'funding_status': 1, 'grade_level': 1}
var getSubjects = function(done) {
cache.get('subjects', function(err, data){
if(err) return done(err)
if(data)
return done(null, JSON.parse(data))
Subjects.find({}, subjectQueryParams, function(err, results) {
if(err) return done(err)
if(results) {
cache.set('subjects', JSON.stringify(results))
}
done(null, results) // no results
})
})
}
app.get('/api/data', function(req, res) {
getSubjects(function(err, data) {
if (err) return res.send(err);
res.json(data);
});
@janajri
Copy link
Author

janajri commented Oct 30, 2015

Also, might want to consider using promises

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