用mysql表达会议并续集
在我的节点/Express服务器上实现会话功能,我正在尝试远离内存会话,我决定将我的应用程序数据库(MySQL)用作会话存储(而不是像Redis这样的专业人士,以降低复杂性) 。
我正在使用以下库:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会话对象已加载(异步)并在请求到达我们的路由/端点之前设置。
操作
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.