Skip to content

Commit

Permalink
feat(server): add store based on redis@4
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Apr 6, 2023
1 parent fcf32cc commit f27e076
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,4 @@ export function instrument(io: Server, opts: Partial<InstrumentOptions>) {
initStatsEmitter(adminNamespace, options.serverId);
}

export { InMemoryStore, RedisStore } from "./stores";
export { InMemoryStore, RedisStore, RedisV4Store } from "./stores";
29 changes: 29 additions & 0 deletions lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,32 @@ export class RedisStore extends Store {
.exec();
}
}

export class RedisV4Store extends Store {
private options: RedisStoreOptions;

constructor(readonly redisClient: any, options?: Partial<RedisStoreOptions>) {
super();
this.options = Object.assign(
{
prefix: "socket.io-admin",
sessionDuration: 86400,
},
options
);
}

private computeKey(sessionId: string) {
return `${this.options.prefix}#${sessionId}`;
}

override doesSessionExist(sessionId: string): Promise<boolean> {
return this.redisClient.exists(this.computeKey(sessionId));
}

override saveSession(sessionId: string) {
return this.redisClient.set(this.computeKey(sessionId), "1", {
EX: this.options.sessionDuration,
});
}
}
Loading

0 comments on commit f27e076

Please sign in to comment.