Skip to content

Commit 76cc43e

Browse files
feat: Cache flags and components for one hour (#114)
This PR adds a caching layer to the background service worker for the flags and components fetchers. The cache just uses localstorage and will start with a one hour TTL. Closes #110
1 parent 6dbf4e0 commit 76cc43e

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/constants.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
export const codecovApiTokenStorageKey = "self_hosted_codecov_api_token"; // Keeping this key to not break existing installs.
1+
export const codecovApiTokenStorageKey = "self_hosted_codecov_api_token";
22
export const selfHostedCodecovURLStorageKey = "self_hosted_codecov_url";
33
export const selfHostedGitHubURLStorageKey = "self_hosted_github_url";
44
export const dynamicContentScriptRegistrationId = "dynamic-content-script";
55

66
export const codecovCloudApiUrl = "https://api.codecov.io";
77
export const githubCloudUrl = "https://github.com";
88

9+
export const cacheTtlMs = 1000 * 60 * 60; // 1 hour
10+
911
export const providers = {
1012
github: "github",
1113
githubEnterprise: "github_enterprise",

src/service.ts

+69
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
selfHostedCodecovURLStorageKey,
99
selfHostedGitHubURLStorageKey,
1010
providers,
11+
cacheTtlMs,
1112
} from "src/constants";
1213

1314
export class Codecov {
@@ -137,10 +138,65 @@ export class Codecov {
137138
};
138139
}
139140

141+
async getCached(
142+
type: "flags" | "components",
143+
owner: string,
144+
repo: string
145+
): Promise<any> {
146+
const cacheKey = `${owner}/${repo}/${type}`;
147+
const cacheExpiryKey = `${owner}/${repo}/${type}/expiry`;
148+
149+
const storage = await browser.storage.local.get([cacheKey, cacheExpiryKey]);
150+
151+
if (!storage[cacheKey] || !storage[cacheExpiryKey]) {
152+
// Cache is not set
153+
return null;
154+
}
155+
156+
const value = JSON.parse(storage[cacheKey]);
157+
const expiry = storage[cacheExpiryKey];
158+
159+
if (Date.now() <= expiry) {
160+
// Cache is valid, return cached value
161+
return value;
162+
}
163+
164+
// Cache is expired, clear cache
165+
await browser.storage.local.remove([cacheKey, cacheExpiryKey]);
166+
167+
return null;
168+
}
169+
170+
async setCached(
171+
type: "flags" | "components",
172+
owner: string,
173+
repo: string,
174+
data: any
175+
): Promise<void> {
176+
const cacheKey = `${owner}/${repo}/${type}`;
177+
const cacheExpiryKey = `${owner}/${repo}/${type}/expiry`;
178+
179+
await browser.storage.local.set({
180+
[cacheKey]: JSON.stringify(data),
181+
[cacheExpiryKey]: Date.now() + cacheTtlMs,
182+
});
183+
184+
return;
185+
}
186+
140187
async listFlags(payload: any, referrer: string): Promise<any> {
141188
await this.init();
142189
const { owner, repo } = payload;
143190

191+
const cachedFlags = await this.getCached("flags", owner, repo);
192+
193+
if (cachedFlags != null) {
194+
return {
195+
ok: true,
196+
data: cachedFlags,
197+
};
198+
}
199+
144200
const url = new URL(
145201
`/api/v2/${this.provider}/${owner}/repos/${repo}/flags`,
146202
this.apiUrl
@@ -153,6 +209,8 @@ export class Codecov {
153209
});
154210
const data = await response.json();
155211

212+
await this.setCached("flags", owner, repo, data);
213+
156214
return {
157215
ok: response.ok,
158216
data,
@@ -163,6 +221,15 @@ export class Codecov {
163221
await this.init();
164222
const { owner, repo } = payload;
165223

224+
const cachedComponents = await this.getCached("components", owner, repo);
225+
226+
if (cachedComponents != null) {
227+
return {
228+
ok: true,
229+
data: cachedComponents,
230+
};
231+
}
232+
166233
const url = new URL(
167234
`/api/v2/${this.provider}/${owner}/repos/${repo}/components`,
168235
this.apiUrl
@@ -175,6 +242,8 @@ export class Codecov {
175242
});
176243
const data = await response.json();
177244

245+
await this.setCached("components", owner, repo, data);
246+
178247
return {
179248
ok: response.ok,
180249
data,

0 commit comments

Comments
 (0)