diff --git a/index.js b/index.js index 57a0a72..af0aee9 100644 --- a/index.js +++ b/index.js @@ -7,21 +7,15 @@ import { uuid, sha1 } from 'crypto.js' import RedisStore from './lib/redis-store.js' -import MemStore from './lib/mem-store.js' // 会话安装包 export const sessionPackage = { name: 'session', install() { var session = this.get('session') - // 这里只创建session的存储器, 而初始化操作在中间件中进行 - if (session.type === 'redis') { - return new RedisStore(session) - } else { - return new MemStore(session) - } - } + return new RedisStore(session) + }, } // 会话中间件 diff --git a/lib/mem-store.js b/lib/mem-store.js deleted file mode 100644 index 3dec72b..0000000 --- a/lib/mem-store.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * 内存版会话管理(仅用于测试,数据) - * @author yutent - * @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] = {} - } -} diff --git a/package.json b/package.json index c87a587..cba8bda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gm5/session", - "version": "1.0.1", + "version": "1.1.0", "type": "module", "description": "会话中间件。", "main": "index.js",