如何从Redis保存和检索会话
我正在尝试将Redis会话集成到以Node.js编写的身份验证系统中.
I am trying to integrate Redis sessions into my authentication system written in Node.js.
我已经能够成功设置Redis服务器,connect-redis
和Express服务器.
I have been able to successfully set up Redis server, connect-redis
and Express server.
这是我的设置(仅此重要):
Here is my setup (just the important bit):
var express = require("express");
var RedisStore = require("connect-redis")(express);
var redis = require("redis").createClient();
app.use(express.cookieParser());
app.use(express.session({
secret: "thisismysecretkey",
store: new RedisStore({ host: 'localhost', port: 6379, client: redis })
}));
现在...我实际上如何创建,读取和销毁会话?我已经阅读了很多关于如何设置connect-redis
的文章,以及关于SO的许多问题,但是我发誓每个都只停留在配置上,而没有解释如何实际使用它...
Now... How do I actually create, read and destroy the session? I have read tons of articles on how to setup connect-redis
and many questions here on SO, but I swear each one stops on just the configuration and does not explain how to actually use it...
我知道这可能非常简单,但是请不要投票,只解释一下:).
I am aware that that is probably extremely simple, but please don't downvote and just explain :).
这应该就是它的全部了.您可以通过req.session
在路由处理程序中访问会话.这些会话会自动创建,保存和销毁.
That should be all there is to it. You access the session in your route handlers via req.session
. The sessions are created, saved, and destroyed automatically.
如果您需要为用户手动创建新会话,请致电req.session.regenerate()
.
If you need to manually create a new session for a user, call req.session.regenerate()
.
如果需要手动保存,可以致电req.session.save()
.
If you need to save it manually, you can call req.session.save()
.
如果需要手动销毁它,可以致电req.session.destroy()
.
If you need to destroy it manually, you can call req.session.destroy()
.
有关方法和属性的完整列表,请参见连接文档.
See the Connect documentation for the full list of methods and properties.