-
Notifications
You must be signed in to change notification settings - Fork 7
/
RedisStore.ts
42 lines (34 loc) · 1.21 KB
/
RedisStore.ts
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
38
39
40
41
42
import Store from './Store.ts'
import type { Redis } from 'https://deno.land/x/redis@v0.27.0/mod.ts'
import { SessionData } from '../Session.ts'
export default class RedisStore implements Store {
keyPrefix : string
db : Redis
constructor(db : Redis, keyPrefix = 'session_') {
this.keyPrefix = keyPrefix
this.db = db
}
async sessionExists(sessionId : string) {
const session = await this.db.get(this.keyPrefix + sessionId)
return session ? true : false
}
async getSessionById(sessionId : string) {
const session = await this.db.get(this.keyPrefix + sessionId)
if (session) {
const sessionString = String(await this.db.get(this.keyPrefix + sessionId))
const value = JSON.parse(sessionString) as SessionData
return value
} else {
return null
}
}
async createSession(sessionId : string, initialData: SessionData) {
await this.db.set(this.keyPrefix + sessionId, JSON.stringify(initialData))
}
async deleteSession(sessionId : string) {
await this.db.del(this.keyPrefix + sessionId)
}
async persistSessionData(sessionId : string, sessionData : SessionData) {
await this.db.set(this.keyPrefix + sessionId, JSON.stringify(sessionData))
}
}