Skip to content

Commit

Permalink
feat(author): cache commit authors for an hour
Browse files Browse the repository at this point in the history
so that we don't keep on doing the same API calls to github
  • Loading branch information
tsimbalar committed Jan 14, 2021
1 parent db09a94 commit c348d87
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/composition-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConstructorFunction, IControllerFactory } from './api/ioc/IControllerFa
import { MetaInfo, meta as metaFromPackageJson } from './meta';
import { BearerAuthenticationProvider } from './api/auth/BearerAuthenticationProvider';
import { BuildInfoController } from './api/controllers/BuildInfoController';
import { CachedCommitAuthorRepository } from './infra/caching/CachedCommitAuthorRepository';
import { CachedRepoRepository } from './infra/caching/CachedRepoRepository';
import { CommitAuthorRepository } from './infra/github/CommitAuthorRepository';
import { Controller } from '@tsoa/runtime';
Expand All @@ -21,9 +22,6 @@ import { UserRepository } from './infra/github/UserRepository';
import { WorkflowRunRepository } from './infra/github/WorkflowRunRepository';
import { getOctokitFactory } from './infra/github/OctokitFactory';

const SERVER_PREFIX = 'gha-build-monitor';
const SERVER_NAME = 'gha-build-monitor';

export interface ApiDependencies {
readonly userRepo: IUserRepository;
readonly repoRepo: IRepoRepository;
Expand Down Expand Up @@ -55,7 +53,10 @@ export class CompositionRoot implements IControllerFactory {
repoRepo: new RepoRepository(octokitFactory),
workflowRunRepo: new WorkflowRunRepository(
octokitFactory,
new CommitAuthorRepository(octokitFactory)
new CachedCommitAuthorRepository(
new LRUCache<string, any>({}),
new CommitAuthorRepository(octokitFactory)
)
),
});
}
Expand Down
27 changes: 27 additions & 0 deletions src/infra/caching/CachedCommitAuthorRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CommitAuthor, ICommitAuthorRepository } from '../github/CommitAuthorRepository';
import LRUCache from 'lru-cache';
import { RepoName } from '../../domain/IRepoRepository';

export class CachedCommitAuthorRepository implements ICommitAuthorRepository {
public constructor(
private readonly cache: LRUCache<string, any>,
private readonly wrapped: ICommitAuthorRepository
) {}

public async getAuthorForCommit(
token: string,
repoName: RepoName,
commitId: string
): Promise<CommitAuthor | null> {
const cacheKey = `${repoName.fullName}/commit(${encodeURIComponent(commitId)})`;
const existing = this.cache.get(cacheKey);

if (existing !== undefined) {
return existing as CommitAuthor | null;
}

const actual = await this.wrapped.getAuthorForCommit(token, repoName, commitId);
this.cache.set(cacheKey, actual, 60 * 60 * 1000); // cache for an hour
return actual;
}
}

0 comments on commit c348d87

Please sign in to comment.