用mysql表达会议并续集

发布于 2025-02-02 18:12:45 字数 1152 浏览 2 评论 0原文

在我的节点/Express服务器上实现会话功能,我正在尝试远离内存会话,我决定将我的应用程序数据库(MySQL)用作会话存储(而不是像Redis这样的专业人士,以降低复杂性) 。

我正在使用以下库:

Express-Session库

connect-session-seque-seperize

第一个是节点/Express应用程序中会话的标准会话实现。虽然第二个允许我们利用我们的应用程序数据库和ORM坚持会话数据。

以下片段来自Express Session文档:

// Access the session as req.session
app.get('/', function(req, res, next) {
  if (req.session.views) {
    req.session.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('<p>views: ' + req.session.views + '</p>')
    res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
    res.end()
  } else {
    req.session.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})

请考虑以下行:

req.session.views = 1

我的问题是,此操作是否异步?在使用内存内会话时,这可能不是问题,但可以通过数据库进行工作与我们的情况一样,连接如果不是异步,这将是瓶颈。

Implementing session functionality to my Node/Express server, I am trying to move away from in-memory sessions, I have decided to use my application database (MySQL) as the session store (as opposed to something specialized like Redis, to reduce complexity).

I am using the following libraries:

express-session Library

connect-session-sequelize

The first one is the standard session implementation for sessions in Node/Express applications. While the second one allows us to leverage our application database and ORM to persist session data.

The following snippet is from express-session documentation:

// Access the session as req.session
app.get('/', function(req, res, next) {
  if (req.session.views) {
    req.session.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('<p>views: ' + req.session.views + '</p>')
    res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
    res.end()
  } else {
    req.session.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})

Please consider the line:

req.session.views = 1

My question is, is this operation asynchronous? When working with in-memory sessions this might not be an issue, but working over a database connection, as in our case, this will be a bottleneck if it is not async.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-02-09 18:12:45

会话对象已加载(异步)并在请求到达我们的路由/端点之前设置。

操作
req.session.views = 1
上方仅在应用程序内存中突变对象。

一旦我们的路由处理程序发送响应,会话对象(以及我们的所有更改)再次在会话存储中持续存在。

The session object is loaded (asynchronously) and set on the request BEFORE it reaches our route/endpoint.

The operation
req.session.views = 1
above only mutates the object in application memory.

Once our route handler sends the response, the session object (along with all of our changes) is persisted on the session store again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文