added server cache to gitea
This commit is contained in:
@@ -9,6 +9,16 @@ export interface GitCommit {
|
||||
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) {
|
||||
let response = await fetch(
|
||||
'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;
|
||||
}
|
||||
|
||||
export async function getLatestCommits(
|
||||
async function fetchLatestCommits(
|
||||
fetch: typeof globalThis.fetch,
|
||||
count: number
|
||||
): Promise<GitCommit[]> {
|
||||
@@ -72,3 +82,41 @@ export async function getLatestCommits(
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user