4

我的 express 应用程序使用 PassportJS 来存储身份验证会话,但它在 redis 存储中创建了数量可笑的密钥,并且所有密钥看起来都一样:

"{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}"

他们确实有 TTL,但商店的规模正在无缘无故地增长。知道为什么要创建这些吗?我的钥匙看起来像:

"sess:8R3A-k6dARJvxXFdAXr5nTG7MeC7JTxb"
"sess:s4VYC-k-nmfSf7n-qGQJimFmt30EYNDp"
"sess:BS7WO92Nyl5R0wAbJ-Vo9o8w1apu0kp7"
"sess:0B1AKS6-MCclPvOXV0nlvNio8U8fxyQO"
"sess:v0UWf60LMwKmMVZgo4RWumX313yPsiD0"

如果我只是观察一下,大约每秒或两秒钟就会创建大约 10 个键。

这是我的会话代码的样子:

var express = require('express'),
    ....
    session = require('express-session'),
    redisStore = require('connect-redis')(session);

...
    app.use(express.static(path.resolve('./public')));

    //Redis
    var redisClient = redisHelper.init();

    app.use(session({
        secret: '...',
        store: new redisStore({
            client: redisClient,
            ttl: 86400
        }),
        resave: false,
        saveUninitialized: false,
        cookie:{maxAge:86400}
    }));

    //Passport
    app.use(passport.initialize());
    app.use(passport.session());
...

Redis init 函数返回一个 redis 客户端的实例:

exports.init = function () {
    redisClient = redis.createClient(config.redis.port, config.redis.server, {});

    redisClient.auth(config.redis.auth);

    redisClient.on('error', function (error) {
        //TODO: log the error
        winston.error('Error while talking to Redis', {message: error});
    });

    return redisClient;
};
4

0 回答 0