Skip to content

Commit

Permalink
fix: models Cache class functionality extended to automatically conne…
Browse files Browse the repository at this point in the history
…ct/disconnect client.
  • Loading branch information
mmoehabb committed Dec 5, 2024
1 parent 008469c commit cf715eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
52 changes: 43 additions & 9 deletions packages/models/src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,60 @@ 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;
public _tutors: Tutors;
public _rules: Rules;
public _peer: Peer;
public _call: Call;

private readonly client: RedisClient;
private disconnectTimeout: NodeJS.Timeout | null = null;

constructor(url: string) {
const client = createClient({ url });
this.client = client;
this.tutors = new Tutors(client);
this.rules = new Rules(client);
this.peer = new Peer(client);
this.call = new Call(client);
this._tutors = new Tutors(client);
this._rules = new Rules(client);
this._peer = new Peer(client);
this._call = new Call(client);
}

get call(): Call {
this.connect();
return this._call;
}
get peer(): Peer {
this.connect();
return this._peer;
}
get rules(): Rules {
this.connect();
return this._rules;
}
get tutors(): Tutors {
this.connect();
return this._tutors;
}

async flush() {
await this.connect();
await this.client.flushAll();
}

async connect() {
await this.client.connect();
if (this.disconnectTimeout === null) {
await this.client.connect();
}
else {
clearTimeout(this.disconnectTimeout);
}
this.disconnectTimeout = setTimeout(() => this.disconnect(), 10_000);
}

async disconnect() {
if (this.disconnectTimeout) {
clearTimeout(this.disconnectTimeout);
this.disconnectTimeout = null;
}
await this.client.quit();
}
}
1 change: 0 additions & 1 deletion services/server/src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ import { redisUrl } from "@/constants";
import { Cache } from "@litespace/models";

export const cache = new Cache(redisUrl);
cache.connect();
5 changes: 3 additions & 2 deletions services/server/tests/api/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { cache } from "@/lib/cache";
describe("/api/v1/call", () => {
beforeEach(async () => {
await flush();
await cache.flush();
});

it("should retrieve call by callId", async () => {
Expand Down Expand Up @@ -37,8 +38,8 @@ describe("/api/v1/call", () => {

// test results
expect(membersIds).to.have.length(1);
expect(membersIds[0]).deep.equal(call.id);
expect(membersIds[0]).deep.equal(tutor.id);
expect(membersIds).to.contains(call.id);
expect(membersIds).to.contains(tutor.id);
});

it("should NOT retrieve call data for an uneligable user", async () => {
Expand Down

0 comments on commit cf715eb

Please sign in to comment.