Skip to content

Commit

Permalink
fix: disable cache by default
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Jan 29, 2025
1 parent 5f8372c commit dd63bfd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
19 changes: 16 additions & 3 deletions packages/gitlab/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ export interface Config {
useOAuth?: boolean;

/**
* Cache TTL for the Gitlab plugin in ms
* @default 5 * 60 * 1000 (5 minutes)
* Cache configuration
* @visibility frontend
*/
cacheTTL?: number;
cache?: {
/**
* Enable caching for the Gitlab plugin
* @default false
* @visibility frontend
*/
enabled?: boolean;

/**
* Cache TTL for the Gitlab plugin in seconds
* @default 300
* @visibility frontend
*/
ttl?: number;
};
};
}
16 changes: 13 additions & 3 deletions packages/gitlab/src/api/GitlabCIClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('GitlabCIClient', () => {
expect(client.codeOwnersPath).toBe('CODEOWNERS');
expect(client.readmePath).toBe('README.md');
expect(client.useOAth).toBe(false);
expect(client.cacheTTL).toBe(5 * 60 * 1000);
expect(client.cacheTTL).toBe(undefined);
});

it('should initialize with custom values', () => {
Expand All @@ -52,12 +52,16 @@ describe('GitlabCIClient', () => {
codeOwnersPath: 'custom/OWNERS',
readmePath: 'docs/README.md',
useOAuth: true,
cacheTTL: 1000,
cache: {
enabled: true,
ttl: 1,
},
});

expect(client.codeOwnersPath).toBe('custom/OWNERS');
expect(client.readmePath).toBe('docs/README.md');
expect(client.useOAth).toBe(true);
expect(client.cacheEnabled).toBe(true);
expect(client.cacheTTL).toBe(1000);
});
});
Expand Down Expand Up @@ -244,7 +248,13 @@ describe('GitlabCIClient', () => {
global.fetch = mockFetch;

beforeEach(() => {
client = new GitlabCIClient({ ...defaultOptions, cacheTTL: 1000 });
client = new GitlabCIClient({
...defaultOptions,
cache: {
enabled: true,
ttl: 1,
},
});
mockFetch.mockReset();
});

Expand Down
24 changes: 17 additions & 7 deletions packages/gitlab/src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export type APIOptions = {
readmePath?: string;
gitlabAuthApi: OAuthApi;
useOAuth?: boolean;
cacheTTL?: number;
cache?: {
enabled?: boolean;
ttl?: number;
};
httpFetch?: typeof fetch;
};

Expand All @@ -44,7 +47,8 @@ export class GitlabCIClient implements GitlabCIApi {
codeOwnersPath: string;
gitlabInstance: string;
readmePath: string;
cacheTTL: number;
cacheTTL?: number;
cacheEnabled: boolean;
httpFetch: typeof fetch;

constructor({
Expand All @@ -54,7 +58,7 @@ export class GitlabCIClient implements GitlabCIApi {
readmePath,
gitlabAuthApi,
gitlabInstance,
cacheTTL,
cache,
useOAuth,
httpFetch = window.fetch.bind(window),
}: APIOptions & { gitlabInstance: string }) {
Expand All @@ -65,7 +69,11 @@ export class GitlabCIClient implements GitlabCIApi {
this.identityApi = identityApi;
this.gitlabAuthApi = gitlabAuthApi;
this.useOAth = useOAuth ?? false;
this.cacheTTL = cacheTTL ?? 5 * 60 * 1000; // 5 minutes cache TTL
this.cacheEnabled = cache?.enabled ?? false;
// Default TTL is 5 minutes, convert to milliseconds
this.cacheTTL = this.cacheEnabled
? (cache?.ttl ?? 60 * 5) * 1000
: undefined;
this.httpFetch = httpFetch;
this.cleanupExpiredCache();
}
Expand All @@ -77,7 +85,7 @@ export class GitlabCIClient implements GitlabCIApi {
readmePath,
gitlabAuthApi,
useOAuth,
cacheTTL,
cache,
}: APIOptions) {
return {
build: (gitlabInstance: string) =>
Expand All @@ -89,12 +97,13 @@ export class GitlabCIClient implements GitlabCIApi {
gitlabInstance,
gitlabAuthApi,
useOAuth,
cacheTTL,
cache,
}),
};
}

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

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

private setCachedData<T>(key: string, data: T): void {
if (!this.cacheEnabled) return;
localStorage.setItem(
key,
JSON.stringify({
Expand All @@ -156,7 +167,6 @@ export class GitlabCIClient implements GitlabCIApi {
const cacheKey = this.getCacheKey(path, query, APIkind);
const cachedData = this.getCachedData<T>(cacheKey);
if (cachedData) {
console.log('hit cache');
return cachedData;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/gitlab/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export const gitlabPlugin = createPlugin({
),
gitlabAuthApi,
useOAuth: configApi.getOptionalBoolean('gitlab.useOAuth'),
cacheTTL: configApi.getOptionalNumber('gitlab.cacheTTL'),
cache: {
enabled: configApi.getOptionalBoolean(
'gitlab.cache.enabled'
),
ttl: configApi.getOptionalNumber('gitlab.cache.ttl'),
},
}),
}),
],
Expand Down

0 comments on commit dd63bfd

Please sign in to comment.