From 99b2c268b2ea3fc9d6448efeaf68ef0f1717bbec Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Thu, 26 Feb 2015 18:32:59 -0800 Subject: [PATCH] Also caching Aggregates. --- server/commons/database.coffee | 6 +++++- server/levels/level_handler.coffee | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/server/commons/database.coffee b/server/commons/database.coffee index 65b06159a..13a485ab4 100644 --- a/server/commons/database.coffee +++ b/server/commons/database.coffee @@ -14,7 +14,11 @@ module.exports.connect = () -> mongoose.connect address mongoose.connection.once 'open', -> Grid.gfs = Grid(mongoose.connection.db, mongoose.mongo) - mongooseCache.install(mongoose, {max: 200, maxAge: 1 * 60 * 1000, debug: false}) + # Hack around Mongoose not exporting Aggregate so that we can patch its exec, too + # https://github.com/LearnBoost/mongoose/issues/1910 + Level = require '../levels/Level' + Aggregate = Level.aggregate().constructor + mongooseCache.install(mongoose, {max: 200, maxAge: 1 * 60 * 1000, debug: false}, Aggregate) module.exports.generateMongoConnectionString = -> if not testing and config.mongo.mongoose_replica_string diff --git a/server/levels/level_handler.coffee b/server/levels/level_handler.coffee index d3060c173..11bdbd4ce 100644 --- a/server/levels/level_handler.coffee +++ b/server/levels/level_handler.coffee @@ -160,13 +160,13 @@ LevelHandler = class LevelHandler extends Handler if err then @sendDatabaseError(res, err) else @sendSuccess res, results getHistogramData: (req, res, slug) -> - query = Session.aggregate [ + aggregate = Session.aggregate [ {$match: {'levelID': slug, 'submitted': true, 'team': req.query.team}} {$project: {totalScore: 1, _id: 0}} ] - #query.cache() # TODO: implement caching for aggregates + aggregate.cache() - query.exec (err, data) => + aggregate.exec (err, data) => if err? then return @sendDatabaseError res, err valueArray = _.pluck data, 'totalScore' @sendSuccess res, valueArray @@ -290,11 +290,11 @@ LevelHandler = class LevelHandler extends Handler sessionQueryParams['team'] = team aggregate = Session.aggregate [ {$match: sessionQueryParams} - {$project: {'totalScore': 1}} {$sort: {'totalScore': -1}} {$limit: 20} + {$project: {'totalScore': 1}} ] - #aggregate.cache() # TODO: implement caching for aggregates + aggregate.cache() aggregate.exec cb async.map teams, findTop20Players.bind(@, sessionsQueryParameters), (err, map) =>