Skip to content

Commit 3345653

Browse files
author
Anush Kamble
committed
fix(cache): include both from and to dates in contribution cache key
Fixes JhaSourav07#1367
1 parent 6ede310 commit 3345653

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

lib/github.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,16 @@ describe('cacheKey', () => {
10851085
it('creates key with year', () => {
10861086
expect(cacheKey('contributions', 'DeepSikha', '2025')).toBe('contributions:deepsikha:2025');
10871087
});
1088+
1089+
it('creates key with from and to date range', () => {
1090+
expect(cacheKey('contributions', 'octocat', '2024-01-01', '2024-06-30')).toBe('contributions:octocat:2024-01-01:2024-06-30');
1091+
});
1092+
1093+
it('collision test: different to dates produce different keys', () => {
1094+
const keyA = cacheKey('contributions', 'octocat', '2024-01-01', '2024-06-30');
1095+
const keyB = cacheKey('contributions', 'octocat', '2024-01-01', '2024-12-31');
1096+
expect(keyA).not.toBe(keyB);
1097+
});
10881098
});
10891099
describe('buildInsights', () => {
10901100
it('uses active streak message when current streak > 3', () => {

lib/github.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,25 @@ export function cacheKey(
212212
kind: 'contributions' | 'profile' | 'repos',
213213
username: string,
214214
year?: string
215+
): string;
216+
export function cacheKey(
217+
kind: 'contributions' | 'profile' | 'repos',
218+
username: string,
219+
from?: string,
220+
to?: string
221+
): string;
222+
export function cacheKey(
223+
kind: 'contributions' | 'profile' | 'repos',
224+
username: string,
225+
yearOrFrom?: string,
226+
to?: string
215227
): string {
216-
return year ? `${kind}:${username.toLowerCase()}:${year}` : `${kind}:${username.toLowerCase()}`;
228+
if (yearOrFrom && to) {
229+
return `${kind}:${username.toLowerCase()}:${yearOrFrom.substring(0, 10)}:${to.substring(0, 10)}`;
230+
}
231+
return yearOrFrom
232+
? `${kind}:${username.toLowerCase()}:${yearOrFrom.substring(0, 4)}`
233+
: `${kind}:${username.toLowerCase()}`;
217234
}
218235

219236
export function clearGitHubApiCacheForTests(): void {
@@ -272,7 +289,7 @@ export async function fetchGitHubContributions(
272289
username: string,
273290
options: FetchOptions = {}
274291
): Promise<ContributionCalendar> {
275-
const key = cacheKey('contributions', username, options.from?.substring(0, 4));
292+
const key = cacheKey('contributions', username, options.from, options.to);
276293
if (!options.bypassCache) {
277294
const cached = await contributionsCache.get(key);
278295
if (cached) return cached;

0 commit comments

Comments
 (0)