diff --git a/index.js b/index.js index 57a0a72..739f600 100644 --- a/index.js +++ b/index.js @@ -7,20 +7,14 @@ 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 0dc8f6b..0000000 --- a/lib/mem-store.js +++ /dev/null @@ -1,64 +0,0 @@ -/** - * 内存版会话管理(仅用于测试,数据) - * @author yutent - * @date 2020/09/18 16:35:26 - */ - -function hideProperty(host, name, value) { - Object.defineProperty(host, name, { - value: value, - writable: true, - enumerable: false, - configurable: true - }) -} - -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有效期 - hideProperty(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 cd86385..47e2698 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { "name": "@gm5/session", - "version": "1.0.1", + "version": "1.1.2", "type": "module", "description": "会话中间件。", "main": "index.js", "author": "yutent ", - "keywords": ["fivejs", "controller", "http"], + "keywords": ["fivejs", "session", "http"], + "dependencies": { + "crypto.js": "^2.0.2", + "ioredis": "^4.17.3" + }, "repository": "https://github.com/bytedo/gmf.session.git", "license": "MIT" }