重构2.0

v2
yutent 2023-10-27 19:18:04 +08:00
parent ecaece0463
commit 7cba707799
2 changed files with 65 additions and 56 deletions

View File

@ -9,7 +9,9 @@ import { uuid, sha1 } from 'crypto.js'
import RedisStore from './lib/redis-store.js' import RedisStore from './lib/redis-store.js'
// 会话安装包 // 会话安装包
export const sessionPackage = { export function createSessionPlugin(){
return {
name: 'session', name: 'session',
install() { install() {
var session = this.get('session') var session = this.get('session')
@ -17,9 +19,11 @@ export const sessionPackage = {
return new RedisStore(session) return new RedisStore(session)
} }
} }
}
// 会话中间件 // 会话中间件
export function sessionConnect(req, res, next) { export function createSession(){
return function (req, res, next) {
var opt = this.get('session') var opt = this.get('session')
var cache = req.cookie('NODESSID') var cache = req.cookie('NODESSID')
var deviceID = '' var deviceID = ''
@ -58,7 +62,8 @@ export function sessionConnect(req, res, next) {
res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain }) res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain })
// 缓存ssid到req上 // 缓存ssid到req上
req.ssid = ssid req.ssid = ssid
this.$$session.start(ssid) this.$$session.update(ssid)
next() next()
} }
}

View File

@ -7,25 +7,29 @@
import Ioredis from 'ioredis' import Ioredis from 'ioredis'
export default class Session { export default class Session {
#store = null
#ttl = 60
constructor(opt) { constructor(opt) {
this.store = new Ioredis({ this.#store = new Ioredis({
host: opt.db.host || '127.0.0.1', host: opt.db.host || '127.0.0.1',
port: opt.db.port || 6379, port: opt.db.port || 6379,
db: opt.db.db || 0 db: opt.db.db || 0
}) })
this.ttl = opt.ttl this.#ttl = opt.ttl
} }
start(ssid) { update(ssid) {
// 设置session有效期 // 设置session有效期
this.store.expire(ssid, this.ttl) this.#store.expire(ssid, this.#ttl)
} }
// 获取session字段值, 需要await指令 // 获取session字段值, 需要await指令
get(ssid, key) { get(ssid, key) {
var defer = Promise.defer() var defer = Promise.defer()
this.store.hgetall(ssid, (err, obj) => { this.#store.hgetall(ssid, (err, obj) => {
if (err) { if (err) {
return defer.reject(err) return defer.reject(err)
} }
@ -37,7 +41,7 @@ export default class Session {
} }
//不传key时,直接返回全部字段 //不传key时,直接返回全部字段
if (key) { if (key) {
defer.resolve(obj.hasOwnProperty(key) ? obj[key] : null) defer.resolve(obj[key])
} else { } else {
defer.resolve(obj) defer.resolve(obj)
} }
@ -49,20 +53,20 @@ export default class Session {
set(ssid, key, val) { set(ssid, key, val) {
if (typeof key === 'object') { if (typeof key === 'object') {
for (let i in key) { for (let i in key) {
this.store.hset(ssid, i, key[i]) this.#store.hset(ssid, i, key[i])
} }
} else { } else {
this.store.hset(ssid, key, val) this.#store.hset(ssid, key, val)
} }
} }
//删除单个字段 //删除单个字段
unset(ssid, key) { unset(ssid, key) {
this.store.hdel(ssid, key) this.#store.hdel(ssid, key)
} }
//清除个人session //清除个人session
clear(ssid) { clear(ssid) {
this.store.del(ssid) this.#store.del(ssid)
} }
} }