7.2 会话序列化

保存会话

1
2
3
4
5
6
7
8
9
10
11
12
export class SessionStorage {
async save(session: Session): Promise<void> {
const filePath = `${this.sessionsDir}/${session.id}.json`;
await fs.writeFile(filePath, JSON.stringify(session, null, 2));
}

async load(sessionId: string): Promise<Session> {
const filePath = `${this.sessionsDir}/${sessionId}.json`;
const content = await fs.readFile(filePath, 'utf-8');
return JSON.parse(content);
}
}

导航

上一篇: 7.1 会话存储方案

下一篇: 7.3 会话管理