Skip to content

Commit 805a62c

Browse files
authored
Merge pull request #704 from immobiliare/fix/disable-cache-by-default
fix: disable cache by default
2 parents 5f8372c + dd63bfd commit 805a62c

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

packages/gitlab/config.d.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ export interface Config {
2222
useOAuth?: boolean;
2323

2424
/**
25-
* Cache TTL for the Gitlab plugin in ms
26-
* @default 5 * 60 * 1000 (5 minutes)
25+
* Cache configuration
2726
* @visibility frontend
2827
*/
29-
cacheTTL?: number;
28+
cache?: {
29+
/**
30+
* Enable caching for the Gitlab plugin
31+
* @default false
32+
* @visibility frontend
33+
*/
34+
enabled?: boolean;
35+
36+
/**
37+
* Cache TTL for the Gitlab plugin in seconds
38+
* @default 300
39+
* @visibility frontend
40+
*/
41+
ttl?: number;
42+
};
3043
};
3144
}

packages/gitlab/src/api/GitlabCIClient.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('GitlabCIClient', () => {
4343
expect(client.codeOwnersPath).toBe('CODEOWNERS');
4444
expect(client.readmePath).toBe('README.md');
4545
expect(client.useOAth).toBe(false);
46-
expect(client.cacheTTL).toBe(5 * 60 * 1000);
46+
expect(client.cacheTTL).toBe(undefined);
4747
});
4848

4949
it('should initialize with custom values', () => {
@@ -52,12 +52,16 @@ describe('GitlabCIClient', () => {
5252
codeOwnersPath: 'custom/OWNERS',
5353
readmePath: 'docs/README.md',
5454
useOAuth: true,
55-
cacheTTL: 1000,
55+
cache: {
56+
enabled: true,
57+
ttl: 1,
58+
},
5659
});
5760

5861
expect(client.codeOwnersPath).toBe('custom/OWNERS');
5962
expect(client.readmePath).toBe('docs/README.md');
6063
expect(client.useOAth).toBe(true);
64+
expect(client.cacheEnabled).toBe(true);
6165
expect(client.cacheTTL).toBe(1000);
6266
});
6367
});
@@ -244,7 +248,13 @@ describe('GitlabCIClient', () => {
244248
global.fetch = mockFetch;
245249

246250
beforeEach(() => {
247-
client = new GitlabCIClient({ ...defaultOptions, cacheTTL: 1000 });
251+
client = new GitlabCIClient({
252+
...defaultOptions,
253+
cache: {
254+
enabled: true,
255+
ttl: 1,
256+
},
257+
});
248258
mockFetch.mockReset();
249259
});
250260

packages/gitlab/src/api/GitlabCIClient.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export type APIOptions = {
3232
readmePath?: string;
3333
gitlabAuthApi: OAuthApi;
3434
useOAuth?: boolean;
35-
cacheTTL?: number;
35+
cache?: {
36+
enabled?: boolean;
37+
ttl?: number;
38+
};
3639
httpFetch?: typeof fetch;
3740
};
3841

@@ -44,7 +47,8 @@ export class GitlabCIClient implements GitlabCIApi {
4447
codeOwnersPath: string;
4548
gitlabInstance: string;
4649
readmePath: string;
47-
cacheTTL: number;
50+
cacheTTL?: number;
51+
cacheEnabled: boolean;
4852
httpFetch: typeof fetch;
4953

5054
constructor({
@@ -54,7 +58,7 @@ export class GitlabCIClient implements GitlabCIApi {
5458
readmePath,
5559
gitlabAuthApi,
5660
gitlabInstance,
57-
cacheTTL,
61+
cache,
5862
useOAuth,
5963
httpFetch = window.fetch.bind(window),
6064
}: APIOptions & { gitlabInstance: string }) {
@@ -65,7 +69,11 @@ export class GitlabCIClient implements GitlabCIApi {
6569
this.identityApi = identityApi;
6670
this.gitlabAuthApi = gitlabAuthApi;
6771
this.useOAth = useOAuth ?? false;
68-
this.cacheTTL = cacheTTL ?? 5 * 60 * 1000; // 5 minutes cache TTL
72+
this.cacheEnabled = cache?.enabled ?? false;
73+
// Default TTL is 5 minutes, convert to milliseconds
74+
this.cacheTTL = this.cacheEnabled
75+
? (cache?.ttl ?? 60 * 5) * 1000
76+
: undefined;
6977
this.httpFetch = httpFetch;
7078
this.cleanupExpiredCache();
7179
}
@@ -77,7 +85,7 @@ export class GitlabCIClient implements GitlabCIApi {
7785
readmePath,
7886
gitlabAuthApi,
7987
useOAuth,
80-
cacheTTL,
88+
cache,
8189
}: APIOptions) {
8290
return {
8391
build: (gitlabInstance: string) =>
@@ -89,12 +97,13 @@ export class GitlabCIClient implements GitlabCIApi {
8997
gitlabInstance,
9098
gitlabAuthApi,
9199
useOAuth,
92-
cacheTTL,
100+
cache,
93101
}),
94102
};
95103
}
96104

97105
private cleanupExpiredCache(): void {
106+
if (!this.cacheEnabled || !this.cacheTTL) return;
98107
const now = Date.now();
99108
for (let i = 0; i < localStorage.length; i++) {
100109
const key = localStorage.key(i);
@@ -122,6 +131,7 @@ export class GitlabCIClient implements GitlabCIApi {
122131
}
123132

124133
private getCachedData<T>(key: string): T | undefined {
134+
if (!this.cacheEnabled || !this.cacheTTL) return undefined;
125135
const cached = localStorage.getItem(key);
126136
if (cached) {
127137
try {
@@ -138,6 +148,7 @@ export class GitlabCIClient implements GitlabCIApi {
138148
}
139149

140150
private setCachedData<T>(key: string, data: T): void {
151+
if (!this.cacheEnabled) return;
141152
localStorage.setItem(
142153
key,
143154
JSON.stringify({
@@ -156,7 +167,6 @@ export class GitlabCIClient implements GitlabCIApi {
156167
const cacheKey = this.getCacheKey(path, query, APIkind);
157168
const cachedData = this.getCachedData<T>(cacheKey);
158169
if (cachedData) {
159-
console.log('hit cache');
160170
return cachedData;
161171
}
162172

packages/gitlab/src/plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ export const gitlabPlugin = createPlugin({
4646
),
4747
gitlabAuthApi,
4848
useOAuth: configApi.getOptionalBoolean('gitlab.useOAuth'),
49-
cacheTTL: configApi.getOptionalNumber('gitlab.cacheTTL'),
49+
cache: {
50+
enabled: configApi.getOptionalBoolean(
51+
'gitlab.cache.enabled'
52+
),
53+
ttl: configApi.getOptionalNumber('gitlab.cache.ttl'),
54+
},
5055
}),
5156
}),
5257
],

0 commit comments

Comments
 (0)