Unverified Commit a7e0812e authored by Mark Stenglein's avatar Mark Stenglein
Browse files

Merge branch 'addRedis' into 1.0

- Added Redis to npm package file
- Added configureation for redis in the project
parents aa9fb182 1972326f
......@@ -4,6 +4,7 @@ var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var mongooseRedisCache = require("mongoose-redis-cache");
// Load site wide configurations
var config = require('./config');
......@@ -31,6 +32,8 @@ app.use(express.static(path.join(__dirname, 'public')));
// Connect to mongo
mongoose.connect(config.mongoDBURL);
mongooseRedisCache(mongoose)
// Populate initial data
var populateDB = require('./setup/populateDB');
populateDB();
......
......@@ -7,7 +7,7 @@ config.siteDescription = "A simple application to add your class schedule to cal
// MongoDB config
config.mongoDBURL = 'mongodb://localhost/schedules';
config.ReloadDB = false;
config.ReloadDB = true;
config.schoolSlugs = {
// Slug: Long Name
......
......@@ -29,6 +29,8 @@ var semesterSchema = new Schema({
]
});
semesterSchema.set('redisCache', true)
// the schema is useless so far
// we need to create a model using it
var Semester = mongoose.model('Semester', semesterSchema);
......
......@@ -12,6 +12,7 @@
"express": "~4.13.4",
"jade": "~1.11.0",
"mongoose": "^4.5.5",
"mongoose-redis-cache": "0.0.3",
"morgan": "~1.7.0",
"node-sass": "^3.8.0",
"node-sass-middleware": "0.8.0"
......
......@@ -24,33 +24,13 @@ router.get('/api/json/classes/:SEMSLUG', function(req, res, next) {
// find each person with matching slug
var query = Semester.findOne({ 'slug': slug });
query.select('-_id -classes._id -classes.session_templates._id');
query.exec(function (err, semester) {
query.lean().exec(function (err, semester) {
if (err || (! semester)) {
res.json({
'results' : 'error, try something different'
})
}
else {
if (jsonSlugCache.slugs.length > 10) {
var leastUsedAmount = jsonSlugCache[jsonSlugCache.slugs[0]].timesUsed;
var leastUsed = 0;
for (var i = 0; i < jsonSlugCache.slugs.length; i++) {
var amountUsed = jsonSlugCache[jsonSlugCache.slugs[i]].timesUsed;
if (amountUsed < leastUsedAmount) {
leastUsedAmount = amountUsed;
leastUsed = i;
}
}
delete jsonSlugCache[jsonSlugCache.slugs[leastUsed]];
jsonSlugCache.slugs.splice(leastUsed, 1);
}
jsonSlugCache[slug] = {
'timesUsed' : 1
}
jsonSlugCache[slug].value = JSON.stringify(semester);
jsonSlugCache.slugs.push(slug);
res.send(jsonSlugCache[slug].value);
}
else {res.json(semester);}
})
}
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment