去除本地存储器

v1^2
宇天 2020-10-06 10:43:28 +08:00
parent 3263ca98d7
commit 7cd378b3ee
3 changed files with 3 additions and 66 deletions

View File

@ -7,21 +7,15 @@
import { uuid, sha1 } from 'crypto.js' import { uuid, sha1 } from 'crypto.js'
import RedisStore from './lib/redis-store.js' import RedisStore from './lib/redis-store.js'
import MemStore from './lib/mem-store.js'
// 会话安装包 // 会话安装包
export const sessionPackage = { export const sessionPackage = {
name: 'session', name: 'session',
install() { install() {
var session = this.get('session') var session = this.get('session')
// 这里只创建session的存储器, 而初始化操作在中间件中进行 // 这里只创建session的存储器, 而初始化操作在中间件中进行
if (session.type === 'redis') { return new RedisStore(session)
return new RedisStore(session) },
} else {
return new MemStore(session)
}
}
} }
// 会话中间件 // 会话中间件

View File

@ -1,57 +0,0 @@
/**
* 内存版会话管理(仅用于测试,数据)
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/18 16:35:26
*/
import fs from 'fs'
export default class Session {
constructor(opt) {
this.store = Object.create(null)
this.ttl = opt.ttl
}
start(ssid) {
var session = this.store[ssid]
// 内存版会话管理, 没有设计计划任务来清理过期数据
// 需要在初始化时先判断, 过期的自动清除, 没过期的, 直接重新续期
if (session) {
if (Date.now() > session.__expires__) {
this.clear(ssid)
}
} else {
session = this.store[ssid] = {}
}
// 设置session有效期
session.__expires__ = Date.now() + this.ttl * 1000
}
// 获取session字段值
get(ssid, key) {
return key ? this.store[ssid][key] || null : this.store[ssid]
}
// 设置session字段值
set(ssid, key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store[ssid][i] = key[i]
}
} else {
this.store[ssid][key] = val
}
}
// 删除单个字段
unset(ssid, key) {
delete this.store[ssid][key]
}
// 清除个人session
clear(ssid) {
this.store[ssid] = {}
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/session", "name": "@gm5/session",
"version": "1.0.1", "version": "1.1.0",
"type": "module", "type": "module",
"description": "会话中间件。", "description": "会话中间件。",
"main": "index.js", "main": "index.js",