using Microsoft.Extensions.Caching.Memory; using Octokit; namespace StabilityMatrix.Core.Helper.Cache; public class GithubApiCache : IGithubApiCache { private readonly IMemoryCache memoryCache; private readonly IGitHubClient githubApi; private readonly TimeSpan cacheDuration = TimeSpan.FromMinutes(5); public GithubApiCache(IMemoryCache memoryCache, IGitHubClient githubApi) { this.memoryCache = memoryCache; this.githubApi = githubApi; } public Task GetLatestRelease(string username, string repository) { var cacheKey = $"LatestRelease-{username}-{repository}"; return memoryCache.GetOrCreateAsync(cacheKey, async entry => { entry.SlidingExpiration = cacheDuration; return await githubApi.Repository.Release.GetLatest(username, repository); })!; } public Task> GetAllReleases(string username, string repository) { var cacheKey = $"Releases-{username}-{repository}"; return memoryCache.GetOrCreateAsync(cacheKey, async entry => { entry.SlidingExpiration = cacheDuration; var allReleases = await githubApi.Repository.Release.GetAll(username, repository); return allReleases.OrderByDescending(x => x.CreatedAt); })!; } public Task> GetAllBranches(string username, string repository) { var cacheKey = $"Branches-{username}-{repository}"; return memoryCache.GetOrCreateAsync(cacheKey, async entry => { entry.SlidingExpiration = cacheDuration; var allReleases = await githubApi.Repository.Branch.GetAll(username, repository); return allReleases; })!; } public Task?> GetAllCommits(string username, string repository, string branch, int page = 1, int perPage = 10) { var cacheKey = $"Commits-{username}-{repository}-{branch}-{page}-{perPage}"; return memoryCache.GetOrCreateAsync(cacheKey, async entry => { entry.SlidingExpiration = cacheDuration; return await githubApi.Repository.Commit.GetAll(username, repository, new CommitRequest {Sha = branch}, new ApiOptions { PageCount = page, PageSize = perPage, StartPage = page }); })!; } }