-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStore.js
37 lines (35 loc) · 1.04 KB
/
Store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const Redis = require("ioredis");
const {Store} = require("koa-session2");
class RedisStore extends Store {
constructor() {
super();
this.redis = new Redis();
}
async get(sid, ctx) {
let data = await this.redis.get(`SESSION:${sid}`);
return JSON.parse(data);
}
async set(session, {
sid = this.getID(24),
maxAge = 1000000
} = {}, ctx) {
try {
// Use redis set EX to automatically drop expired sessions
await this.redis.set(`SESSION:${sid}`, JSON.stringify(session), 'EX', maxAge / 1000);
} catch (e) {}
return sid;
}
async expired(sid,ctx){
let expired = await this.redis.exists(`SESSION:${sid}`);
return expired;
}
//判断剩余时间
async laveTime(sid, ctx) {
let time = await this.redis.ttl(`SESSION:${sid}`);
return time;
}
async destroy(sid, ctx) {
return await this.redis.del(`SESSION:${sid}`);
}
}
module.exports = RedisStore;