From a5d896eea9473ded2701c8097a2fd92642a4189e Mon Sep 17 00:00:00 2001 From: olli Date: Fri, 14 Nov 2025 17:17:30 +0100 Subject: [PATCH] feat: Limit maximum commits and contributions cache duration to optimize Gitea API calls --- src/Controller/GitRepositoryController.php | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/Controller/GitRepositoryController.php b/src/Controller/GitRepositoryController.php index 452ad15..4f2cff0 100644 --- a/src/Controller/GitRepositoryController.php +++ b/src/Controller/GitRepositoryController.php @@ -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