Skip to content

Commit 6ba2163

Browse files
feat(server): add store based on redis@4
Reference: https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md Related: #65
1 parent fcf32cc commit 6ba2163

File tree

6 files changed

+394
-40
lines changed

6 files changed

+394
-40
lines changed

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,4 @@ export function instrument(io: Server, opts: Partial<InstrumentOptions>) {
594594
initStatsEmitter(adminNamespace, options.serverId);
595595
}
596596

597-
export { InMemoryStore, RedisStore } from "./stores";
597+
export { InMemoryStore, RedisStore, RedisV4Store } from "./stores";

lib/stores.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,32 @@ export class RedisStore extends Store {
7070
.exec();
7171
}
7272
}
73+
74+
export class RedisV4Store extends Store {
75+
private options: RedisStoreOptions;
76+
77+
constructor(readonly redisClient: any, options?: Partial<RedisStoreOptions>) {
78+
super();
79+
this.options = Object.assign(
80+
{
81+
prefix: "socket.io-admin",
82+
sessionDuration: 86400,
83+
},
84+
options
85+
);
86+
}
87+
88+
private computeKey(sessionId: string) {
89+
return `${this.options.prefix}#${sessionId}`;
90+
}
91+
92+
doesSessionExist(sessionId: string): Promise<boolean> {
93+
return this.redisClient.exists(this.computeKey(sessionId));
94+
}
95+
96+
saveSession(sessionId: string) {
97+
return this.redisClient.set(this.computeKey(sessionId), "1", {
98+
EX: this.options.sessionDuration,
99+
});
100+
}
101+
}

0 commit comments

Comments
 (0)