Skip to content

Commit 3e56f93

Browse files
committed
refactor: use helpers for making cache keys
1 parent 78e06f0 commit 3e56f93

13 files changed

+37
-31
lines changed

src/configs/cache.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export const cacheConfig = {
22
// TODO TTL should be variable based on the repo activity
33
ttl: 60 * 60 * 24, // Cache for 24 hours
44
key: {
5-
githubUserContributors: `github/${String}:contributors`,
6-
githubUserConfig: `github/${String}:config`,
7-
githubUserApiKey: `github/${String}/${String}:key`
5+
userContributors: `user/${String}:contributors`,
6+
userConfig: `user/${String}:config`,
7+
userApiKey: `user/${String}/${String}:key`
88
}
99
} as const;

src/handlers/app.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export class AppHandler {
4343
// Service initialization
4444
this.services = {
4545
appService: AppService.getInstance(),
46-
configService: ConfigService.getInstance(),
4746
openAiService: OpenAiService.getInstance(),
4847
queueService: QueueService.getInstance(),
49-
cacheService: CacheService.getInstance()
48+
cacheService: CacheService.getInstance(),
49+
configService: new ConfigService(CacheService.getInstance(), QueueService.getInstance())
5050
};
5151

5252
// Handler initialization

src/handlers/contributor.handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Handler } from '~common/interfaces';
44
import { CacheKey } from '~common/types';
55
import { cacheConfig } from '~configs';
66
import { SystemError } from '~errors';
7+
import { getUserContributorsCacheKey } from '~helpers';
78
import { CacheService, GitHubService, QueueService } from '~services';
89

910
export class ContributorHandler implements Handler<'member'> {
@@ -14,7 +15,7 @@ export class ContributorHandler implements Handler<'member'> {
1415

1516
async handle(context: Context<'member'>, apiKey: string): Promise<void> {
1617
const { action, member } = context.payload;
17-
const contributorsCacheKey: CacheKey = `github/${apiKey}:contributors`;
18+
const contributorsCacheKey = getUserContributorsCacheKey(apiKey);
1819

1920
const cachedContributors = await this.cacheService.get<User[]>(contributorsCacheKey);
2021

@@ -28,7 +29,7 @@ export class ContributorHandler implements Handler<'member'> {
2829
}
2930

3031
async safeCacheContributors(context: Context, apiKey: string): Promise<void> {
31-
const contributorsCacheKey: CacheKey = `github/${apiKey}:contributors`;
32+
const contributorsCacheKey = getUserContributorsCacheKey(apiKey);
3233
const cachedContributors = await this.cacheService.get<User[]>(contributorsCacheKey);
3334

3435
if (!cachedContributors) {

src/handlers/installation.handler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Context } from 'probot';
22
import { envs } from '~common/consts';
33
import { Handler } from '~common/interfaces';
4+
import { getUserApiKeyCacheKey, getUserConfigCacheKey, getUserContributorsCacheKey } from '~helpers';
45
import { CacheService } from '~services';
56

67
export class installationHandler implements Handler<'installation'> {
@@ -13,9 +14,9 @@ export class installationHandler implements Handler<'installation'> {
1314

1415
const { owner, repo } = context.repo();
1516

16-
await this.cacheService.delete(`github/${apiKey}:contributors`);
17-
await this.cacheService.delete(`github/${apiKey}:config`);
18-
await this.cacheService.delete(`github/${owner}/${repo}:key`);
17+
await this.cacheService.delete(getUserContributorsCacheKey(apiKey));
18+
await this.cacheService.delete(getUserConfigCacheKey(apiKey));
19+
await this.cacheService.delete(getUserApiKeyCacheKey(owner, repo));
1920
// Delete all user specific keys here
2021
}
2122
}

src/handlers/push.handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Context } from 'probot';
22
import { Handler } from '~common/interfaces';
33
import { appConfig } from '~configs';
4+
import { getUserConfigCacheKey } from '~helpers';
45
import { CacheService, ConfigService } from '~services';
56

67
export class PushHandler implements Handler<'push'> {
@@ -18,7 +19,7 @@ export class PushHandler implements Handler<'push'> {
1819
return;
1920
}
2021

21-
await this.cacheService.delete(`github/${apiKey}:config`);
22+
await this.cacheService.delete(getUserConfigCacheKey(apiKey));
2223
await this.configService.getConfig(context, apiKey);
2324
}
2425
}

src/helpers/cache.helper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { CacheKey } from '~common/types';
2+
3+
export const getUserContributorsCacheKey = (apiKey: string): CacheKey => `user/${apiKey}:contributors`;
4+
5+
export const getUserConfigCacheKey = (apiKey: string): CacheKey => `user/${apiKey}:config`;
6+
7+
export const getUserApiKeyCacheKey = (owner: string, repo: string): CacheKey => `user/${owner}/${repo}:key`;

src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './ai.helper';
2+
export * from './cache.helper';
23
export * from './guard.helper';
34
export * from './hash.helper';
45
export * from './object.helper';

src/services/config.service.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
import { Context } from 'probot';
22
import { defaultConfig } from '~common/consts';
33
import { Severity } from '~common/enums';
4-
import { CacheKey, Config } from '~common/types';
4+
import { Config } from '~common/types';
55
import { appConfig, cacheConfig } from '~configs';
66
import { UserError } from '~errors';
7-
import { decodeBase64ToText, merge, parseYaml } from '~helpers';
7+
import { decodeBase64ToText, getUserApiKeyCacheKey, getUserConfigCacheKey, merge, parseYaml } from '~helpers';
88
import { configSchema } from '~schemas';
99
import { CacheService } from './cache.service';
1010
import { GitHubService } from './github.service';
1111
import { QueueService } from './queue.service';
1212

1313
export class ConfigService {
14-
private static instance: ConfigService;
15-
16-
private constructor(
14+
constructor(
1715
private readonly cacheService: CacheService,
1816
private readonly queueService: QueueService
1917
) {}
2018

21-
public static getInstance(): ConfigService {
22-
if (!ConfigService.instance) {
23-
ConfigService.instance = new ConfigService(CacheService.getInstance(), QueueService.getInstance());
24-
}
25-
26-
return ConfigService.instance;
27-
}
28-
2919
async getApiKey(context: Context): Promise<string> {
3020
try {
3121
const { owner, repo } = context.repo();
32-
const apiKeyCacheKey: CacheKey = `github/${owner}/${repo}:key`;
22+
const apiKeyCacheKey = getUserApiKeyCacheKey(owner, repo);
3323
const cachedApiKey = await this.cacheService.get<string>(apiKeyCacheKey);
3424

3525
if (cachedApiKey) {
@@ -61,7 +51,7 @@ export class ConfigService {
6151
}
6252

6353
async getConfig(context: Context<'pull_request'>, apiKey: string): Promise<Config> {
64-
const configCacheKey: CacheKey = `github/${apiKey}:config`;
54+
const configCacheKey = getUserConfigCacheKey(apiKey);
6555
const cachedConfig = await this.cacheService.get<Config>(configCacheKey);
6656

6757
if (cachedConfig) {

src/utils/chat-skip-conditions.util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Context } from 'probot';
44
import { Config } from '~common/types';
55
import { SystemError } from '~errors';
66
import {
7+
getUserContributorsCacheKey,
78
isDiscussion,
89
isDiscussionComment,
910
isInlineComment,
@@ -53,7 +54,7 @@ export class ChatSkipConditions {
5354
if (contributorsOnly) {
5455
const contributors = await this.cacheService.get<
5556
RestEndpointMethodTypes['repos']['listContributors']['response']['data']
56-
>(`github/${this.apiKey}:contributors`);
57+
>(getUserContributorsCacheKey(this.apiKey));
5758

5859
if (!contributors) {
5960
return true;

src/utils/diagram-skip-conditions.util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods';
22
import { Context } from 'probot';
33
import { Config } from '~common/types';
4+
import { getUserContributorsCacheKey } from '~helpers';
45
import { CacheService } from '~services';
56

67
export class DiagramSkipConditions {
@@ -35,7 +36,7 @@ export class DiagramSkipConditions {
3536
if (contributorsOnly) {
3637
const contributors = await this.cacheService.get<
3738
RestEndpointMethodTypes['repos']['listContributors']['response']['data']
38-
>(`github/${this.apiKey}:contributors`);
39+
>(getUserContributorsCacheKey(this.apiKey));
3940

4041
if (!contributors) {
4142
return true;

0 commit comments

Comments
 (0)