Skip to content

Commit d0d330b

Browse files
committed
✨ Add helper methods (#1995)
1 parent 3792cdb commit d0d330b

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/lib/clients/cache_strategy.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { Cache } from '$lib/clients/cache';
22

3+
import type { ContestsForImport } from '$lib/types/contest';
4+
import type { TasksForImport } from '$lib/types/task';
5+
36
/**
4-
* Utility class for managing cached data for contest tasks.
5-
* Provides a mechanism to either retrieve data from cache if available
6-
* or fetch it using the provided function.
7+
* A strategy for caching contest and task data.
8+
* Separates the caching logic from the data fetching concerns.
79
*/
810
export class ContestTaskCache {
11+
/**
12+
* Constructs a cache strategy with the specified contest and task caches.
13+
* @param contestCache - Cache for storing contest import data
14+
* @param taskCache - Cache for storing task import data
15+
*/
16+
constructor(
17+
private readonly contestCache: Cache<ContestsForImport>,
18+
private readonly taskCache: Cache<TasksForImport>,
19+
) {}
20+
921
/**
1022
* Retrieves data from cache if available, otherwise fetches it using the provided function.
1123
*
@@ -34,6 +46,8 @@ export class ContestTaskCache {
3446
return cachedData;
3547
}
3648

49+
console.log(`Cache miss for ${key}, fetching...`);
50+
3751
try {
3852
const contestTasks = await fetchFunction();
3953
cache.set(key, contestTasks);
@@ -44,4 +58,24 @@ export class ContestTaskCache {
4458
return [] as unknown as T;
4559
}
4660
}
61+
62+
/**
63+
* Gets contests from cache or fetches them.
64+
*/
65+
async getCachedOrFetchContests(
66+
key: string,
67+
fetchFunction: () => Promise<ContestsForImport>,
68+
): Promise<ContestsForImport> {
69+
return this.getCachedOrFetch(key, fetchFunction, this.contestCache);
70+
}
71+
72+
/**
73+
* Gets tasks from cache or fetches them.
74+
*/
75+
async getCachedOrFetchTasks(
76+
key: string,
77+
fetchFunction: () => Promise<TasksForImport>,
78+
): Promise<TasksForImport> {
79+
return this.getCachedOrFetch(key, fetchFunction, this.taskCache);
80+
}
4781
}

0 commit comments

Comments
 (0)