Skip to content

Commit

Permalink
chore(fdr): setup semantic caching (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 24, 2024
1 parent aeaa347 commit 9f1a374
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion servers/fdr/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh
#!/bin/bash
set -e
prisma migrate deploy --schema /app/servers/fdr/prisma/schema.prisma
node --experimental-specifier-resolution=node /app/servers/fdr/dist/server.js
20 changes: 17 additions & 3 deletions servers/fdr/src/__test__/ete/ete.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/* eslint-disable vitest/expect-expect */

const PORT = 8080;

it("check health", async () => {
// register empty definition
await sleep(5000);
const response = await fetch(`http://localhost:${PORT}/health`);
expect(response.status).toEqual(200);

for (let i = 0; i < 10; ++i) {
await sleep(20_000);
try {
const response = await fetch(`http://localhost:${PORT}/health`);
if (response.status === 200) {
return;
} else {
console.log(`Received status ${response.status}`);
}
} catch (err) {
console.log(err);
}
}
throw new Error("Failed to make successfull request");
}, 100_000);

function sleep(ms: number): Promise<void> {
Expand Down
18 changes: 15 additions & 3 deletions servers/fdr/src/services/docs-cache/DocsDefinitionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ export interface DocsDefinitionCache {
invalidateCache(url: URL): Promise<void>;
}

/**
* The semantic version that the current version of FDR expects.
* Please bump this version if you would like to "clear" the cache.
*/
const SEMANTIC_VERSION = "v2";

/**
* All modifications to this type must be forward compatible.
* In other words, only add optional properties.
*/
export interface CachedDocsResponse {
/** Adding a version to the cached response to allow for breaks in the future. */
version: "v2";
version: typeof SEMANTIC_VERSION;
updatedTime: Date;
response: DocsV2Read.LoadDocsForUrlResponse;
dbFiles: Record<DocsV1Read.FileId, DocsV1Db.DbFileInfoV2>;
Expand Down Expand Up @@ -248,10 +254,16 @@ export class DocsDefinitionCacheImpl implements DocsDefinitionCache {
}

private async getDocsForUrlFromCache({ url }: { url: URL }): Promise<CachedDocsResponse | null> {
let record: CachedDocsResponse | null = null;
if (this.redisDocsCache) {
return await this.redisDocsCache.get({ url });
record = await this.redisDocsCache.get({ url });
} else {
record = this.localDocsCache.get({ url }) ?? null;
}
if (record != null && record.version !== SEMANTIC_VERSION) {
return null;
}
return this.localDocsCache.get({ url }) ?? null;
return record;
}

private async getDocsForUrlFromDatabase({ url }: { url: URL }): Promise<CachedDocsResponse> {
Expand Down

0 comments on commit 9f1a374

Please sign in to comment.