我使用Express设置了一个Node服务器,使用Passport进行身份验证.我听说connect-mongo很适合用于持久登录会话,所以我设置好了,一开始看起来一切正常,用户会话会根据过期时间由mongo自动删除.但是,在生产中,对于每个用户会话,还有5000个永不过期的空会话,我无法弄清楚为什么mongo不会自动清理它们. mongo中空会话的示例条目如下所示:
{ "_id" : "JMtV5Z1oWRkgh9KIKlwSqwOE","session" : "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-02-13T22:09:09.948Z\",\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}","expires" : Date( 1392329349948 ) }
这是Express配置代码:
var express = require('express'),passport = require('passport'),fs = require('fs'),http = require('http'),https = require('https'),util = require('util'),mongoose = require('mongoose'),MongoStore = require('connect-mongo')(express),FacebookStrategy = require('passport-facebook').Strategy,LocalStrategy = require('passport-local').Strategy; app.configure(function() { app.set('views',__dirname + '/views'); app.set('view engine','ejs'); app.use(express.logger()); app.use(requireHTTPS); app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.session({ secret: 'asdfasdf',cookie: { maxAge: 24 * 60 * 60 * 1000 },store: new MongoStore({ mongoose_connection: mongoose.connections[0],clear_interval: 3600 },function(err){ console.log(err || 'connect-mongodb setup ok'); } ) })); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); app.use(express.static(__dirname + '/public'),{ maxAge: 31557600000 }); });
我认为这些空会话是从搜索机器人创建的,因为我想不出为什么我的网站会在一夜之间被击中20,000次的任何其他原因.但即便如此,只要会话正确到期就不会出现问题,我的数据库也不会耗尽内存,但它们永远不会被清理干净.
任何见解将不胜感激,谢谢