首页 文章

在快递中为商业应用程序存储会话我应该使用什么?

提问于
浏览
1

我想为同一个帐户创建多个登录的会话现在我正在为任何特定用户存储唯一的会话字符串现在我的问题是,当我使用快速会话时,然后在其文档页面上enter link description here它提到了

Warning: The default server-side session storage, MemoryStore, is purposely not designed for a production environment. It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing

现在我的问题是,因为我正在创建一个商业应用程序,我应该使用它,如果我不使用那么什么是最好的存储会话我听说过redis但我也听说它消耗了很多内存,这就是为什么可以请大家放一些我真的很有意思 .

1 回答

  • 1

    常见的三个更有用的选项而不是 MemoryStore

    CookieSession

    connect-redis

    connect-mongo

    来自here

    我建议你使用 CookieSession ,因为它更简单快速 . 来自docs的简单示例:

    var cookieSession = require('cookie-session')
    var express = require('express')
    
    var app = express()
    
    app.set('trust proxy', 1) // trust first proxy
    
    app.use(cookieSession({
      name: 'session',
      keys: ['key1', 'key2']
    }))
    

    到期时间:

    maxAge:一个数字,表示从Date.now()到期的毫秒数到期时间:一个Date对象,表示cookie的到期日期(默认情况下在会话结束时到期) .

    您可以在属于当前用户的单个cookie上设置expires或maxAge:

    // This user should log in again after restarting the browser
    req.session.cookie.expires = false;
    
    // This user won't have to log in for a year
    req.session.cookie.maxAge = 365 * 24 * 60 * 60 * 1000;
    

相关问题