Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(models): impl. call cache #168

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/models/src/cache/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CacheBase {
public client: RedisClientType<RedisModules, RedisFunctions, RedisScripts>
) {}

encode<T extends object>(values: T): string {
encode<T extends object | string | number>(values: T): string {
return JSON.stringify(values);
}

Expand Down
67 changes: 67 additions & 0 deletions packages/models/src/cache/call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { CacheBase } from "@/cache/base";

export class Call extends CacheBase {
private readonly prefixes = {
call: "call",
user: "user:call",
};
private ttl = 60 * 60; // 1 hour

withTtl(ttl: number): Call {
this.ttl = ttl;
return this;
}

async addMember({ callId, userId }: { callId: number; userId: number }) {
const callKey = this.asCallKey(callId);
const userKey = this.asUserKey(userId);
await this.client
.multi()
.sAdd(callKey, this.encode(userId))
.set(userKey, this.encode(callId))
.expire(callKey, this.ttl)
.expire(userKey, this.ttl)
.exec();
}

async removeMember({ callId, userId }: { callId: number; userId: number }) {
await this.client
.multi()
.sRem(this.asCallKey(callId), userId.toString())
.del(this.asUserKey(userId))
.exec();
}

async removeMemberByUserId(userId: number) {
const result = await this.client.get(this.asUserKey(userId));
if (!result) return;
const callId = this.decode(result) as number;
await this.removeMember({ callId, userId });
}

async getMembers(callId: number): Promise<number[]> {
const result = await this.client.sMembers(this.asCallKey(callId));
return result.map((value) => this.decode(value));
}

async isMember({
callId,
userId,
}: {
callId: number;
userId: number;
}): Promise<boolean> {
return await this.client.sIsMember(
this.asCallKey(callId),
this.encode(userId)
);
}

private asCallKey(callId: number): string {
return `${this.prefixes.call}:${callId}`;
}

private asUserKey(userId: number): string {
return `${this.prefixes.user}:${userId}`;
}
}
3 changes: 3 additions & 0 deletions packages/models/src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Tutors } from "@/cache/tutors";
import { Rules } from "@/cache/rules";
import { RedisClient } from "@/cache/base";
import { Peer } from "@/cache/peer";
import { Call } from "@/cache/call";

export class Cache {
public tutors: Tutors;
public rules: Rules;
public peer: Peer;
public call: Call;
private readonly client: RedisClient;

constructor(url: string) {
Expand All @@ -16,6 +18,7 @@ export class Cache {
this.tutors = new Tutors(client);
this.rules = new Rules(client);
this.peer = new Peer(client);
this.call = new Call(client);
}

async flush() {
Expand Down
1 change: 0 additions & 1 deletion services/server/src/handlers/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { calls } from "@litespace/models";
import { NextFunction, Request, Response } from "express";
import asyncHandler from "express-async-handler";
import { withNamedId } from "@/validation/utils";
import { groupBy } from "lodash";
import { forbidden, notfound } from "@/lib/error";
import { isAdmin, isGhost, isUser } from "@litespace/auth";

Expand Down
Loading