feat: Limit maximum commits and contributions cache duration to optimize Gitea API calls

This commit is contained in:
olli 2025-11-14 17:17:30 +01:00
parent 8a132d2fb9
commit a5d896eea9

View File

@ -31,7 +31,8 @@ class GitRepositoryController extends AbstractController
public function getCommits(GitRepository $gitRepo, Request $request): JsonResponse
{
try {
$limit = (int)$request->query->get('limit', 100);
// Limit maximum commits to reduce slow Gitea API calls
$limit = min((int)$request->query->get('limit', 20), 50);
$branch = $request->query->get('branch', $gitRepo->getBranch() ?? 'main');
// Cache key unique per repository, branch, and limit
@ -43,15 +44,11 @@ class GitRepositoryController extends AbstractController
);
$commits = $this->cache->get($cacheKey, function (ItemInterface $item) use ($gitRepo, $branch, $limit) {
// Cache for 15 minutes (commits change frequently during active development)
$item->expiresAfter(900);
// Cache for 1 hour (Gitea API is very slow, reduce API calls)
$item->expiresAfter(3600);
// Choose service based on provider
if ($gitRepo->getProvider() === 'github') {
return $this->getCommitsFromGitHub($gitRepo, $branch, $limit);
} elseif ($gitRepo->getProvider() === 'gitea') {
return $this->getCommitsFromGitea($gitRepo, $branch, $limit);
} elseif ($gitRepo->getLocalPath()) {
// Prefer local path (fastest) over API calls
if ($gitRepo->getLocalPath()) {
return $this->gitService->getCommits(
$gitRepo->getLocalPath(),
$branch,
@ -59,7 +56,14 @@ class GitRepositoryController extends AbstractController
);
}
throw new \Exception('No data source configured (neither provider API nor local path)');
// Fallback to provider APIs
if ($gitRepo->getProvider() === 'github') {
return $this->getCommitsFromGitHub($gitRepo, $branch, $limit);
} elseif ($gitRepo->getProvider() === 'gitea') {
return $this->getCommitsFromGitea($gitRepo, $branch, $limit);
}
throw new \Exception('No data source configured (neither local path nor provider API)');
});
// Update last sync timestamp
@ -113,15 +117,11 @@ class GitRepositoryController extends AbstractController
);
$contributions = $this->cache->get($cacheKey, function (ItemInterface $item) use ($gitRepo, $branch, $author, $year) {
// Cache for 1 hour (contributions data is relatively stable)
$item->expiresAfter(3600);
// Cache for 6 hours (contributions data is stable, Gitea API is slow)
$item->expiresAfter(21600);
// Choose service based on provider
if ($gitRepo->getProvider() === 'github') {
return $this->getContributionsFromGitHub($gitRepo, $year);
} elseif ($gitRepo->getProvider() === 'gitea') {
return $this->getContributionsFromGitea($gitRepo, $branch, $author, $year);
} elseif ($gitRepo->getLocalPath()) {
// Prefer local path (fastest) over API calls
if ($gitRepo->getLocalPath()) {
return $this->gitService->getContributions(
$gitRepo->getLocalPath(),
$branch,
@ -130,7 +130,14 @@ class GitRepositoryController extends AbstractController
);
}
throw new \Exception('No data source configured (neither provider API nor local path)');
// Fallback to provider APIs
if ($gitRepo->getProvider() === 'github') {
return $this->getContributionsFromGitHub($gitRepo, $year);
} elseif ($gitRepo->getProvider() === 'gitea') {
return $this->getContributionsFromGitea($gitRepo, $branch, $author, $year);
}
throw new \Exception('No data source configured (neither local path nor provider API)');
});
// Update last sync timestamp