added server cache to gitea

This commit is contained in:
2026-11-10 17:39:42 +00:00
parent 2843c5925a
commit a3a418a037

View File

@@ -9,6 +9,16 @@ export interface GitCommit {
user: string; user: string;
} }
const CACHE_TTL = 3 * 60 * 60 * 1000;
interface CacheEntry<T> {
value: T;
expiresAt: number;
}
const commitCache = new Map<string, CacheEntry<GitCommit[]>>();
const pendingRequests = new Map<string, Promise<GitCommit[]>>();
async function getGiteaRepos(fetch: typeof globalThis.fetch) { async function getGiteaRepos(fetch: typeof globalThis.fetch) {
let response = await fetch( let response = await fetch(
'https://git.voidarc.co.uk/api/v1/repos/search?sort=updated&order=desc&limit=10' 'https://git.voidarc.co.uk/api/v1/repos/search?sort=updated&order=desc&limit=10'
@@ -44,7 +54,7 @@ async function getRepoCommits(fetch: typeof globalThis.fetch, url: string) {
return finalData; return finalData;
} }
export async function getLatestCommits( async function fetchLatestCommits(
fetch: typeof globalThis.fetch, fetch: typeof globalThis.fetch,
count: number count: number
): Promise<GitCommit[]> { ): Promise<GitCommit[]> {
@@ -72,3 +82,41 @@ export async function getLatestCommits(
return commitList.slice(0, count); return commitList.slice(0, count);
} }
export async function getLatestCommits(
fetch: typeof globalThis.fetch,
count: number
): Promise<GitCommit[]> {
const cacheKey = String(count);
const cached = commitCache.get(cacheKey);
if (cached && cached.expiresAt > Date.now()) {
return cached.value;
}
const pending = pendingRequests.get(cacheKey);
if (pending) return pending;
const request = fetchLatestCommits(fetch, count)
.then((commits) => {
commitCache.set(cacheKey, {
value: commits,
expiresAt: Date.now() + CACHE_TTL
});
return commits;
})
.finally(() => {
pendingRequests.delete(cacheKey);
});
pendingRequests.set(cacheKey, request);
return request;
}
export function invalidateCommitCache(count: number): void {
commitCache.delete(String(count));
}