Last active
October 30, 2015 15:05
-
-
Save janajri/f7e7d509a7d4c9045073 to your computer and use it in GitHub Desktop.
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 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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, might want to consider using promises