@eggjs/ 会话
彩蛋会话插件,基于 科亚语会话。 。 。 .
使用量
session 是默认启用的彩蛋内置插件.
// {app_root}/config/plugin.js
exports.session = true; // enable by default
外部存储
蛋片段支持外部商店,可以将会话存储在redis,memcacked或其他数据库中.
例如,如果想要将会话存储在 redis 中,则必须:
npm i --save @eggjs/redis
- 导入
@eggjs/redis作为插件并设置配置
// config/plugin.js
exports.redis = {
enable: true,
package: '@eggjs/redis',
};
// config/config.default.js
exports.redis = {
// your redis configurations
};
- 用 redis 执行会话存储
// app.js
module.exports = (app) => {
// set redis session store
// session store must have 3 methods
// define sessionStore in `app.js` so you can access `app.redis`
app.sessionStore = {
async get(key) {
const res = await app.redis.get(key);
if (!res) return null;
return JSON.parse(res);
},
async set(key, value, maxAge) {
// maxAge not present means session cookies
// we can't exactly know the maxAge and just set an appropriate value like one day
if (!maxAge) maxAge = 24 * 60 * 60 * 1000;
value = JSON.stringify(value);
await app.redis.set(key, value, 'PX', maxAge);
},
async destroy(key) {
await app.redis.del(key);
},
};
// session store can be a session store class
// app.sessionStore = class Store {
// constructor(app) {
// this.app = app;
// }
// async get() {}
// async set() {}
// async destroy() {}
// };
};
一旦您使用外部会话商店,会话会话会强烈依赖于您的外部会话商店,如果您的外部会话商店下架,则无法访问会话. 仅在必要时使用外部会话存储, 避免将会话用作缓存, 保持会话的倾斜并用 cookie 存储 !
配置
支持所有配置在 科亚语会话。 。 。 .
- 对数
Support not to print the session value when session event trigger log. Default to be true.
问题和建议
请打开一个问题 这里。 。 。 .
许可证
贡献者
制造的 贡献者-img。 。 。 .