You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.4 KiB
67 lines
2.4 KiB
1 year ago
|
using Microsoft.Extensions.Caching.Memory;
|
||
1 year ago
|
using Octokit;
|
||
1 year ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Helper.Cache;
|
||
1 year ago
|
|
||
|
public class GithubApiCache : IGithubApiCache
|
||
|
{
|
||
|
private readonly IMemoryCache memoryCache;
|
||
1 year ago
|
private readonly IGitHubClient githubApi;
|
||
1 year ago
|
private readonly TimeSpan cacheDuration = TimeSpan.FromMinutes(5);
|
||
|
|
||
1 year ago
|
public GithubApiCache(IMemoryCache memoryCache, IGitHubClient githubApi)
|
||
1 year ago
|
{
|
||
|
this.memoryCache = memoryCache;
|
||
|
this.githubApi = githubApi;
|
||
|
}
|
||
|
|
||
1 year ago
|
public Task<Release> GetLatestRelease(string username, string repository)
|
||
1 year ago
|
{
|
||
|
var cacheKey = $"LatestRelease-{username}-{repository}";
|
||
|
return memoryCache.GetOrCreateAsync(cacheKey, async entry =>
|
||
|
{
|
||
|
entry.SlidingExpiration = cacheDuration;
|
||
1 year ago
|
return await githubApi.Repository.Release.GetLatest(username, repository);
|
||
1 year ago
|
})!;
|
||
|
}
|
||
|
|
||
1 year ago
|
public Task<IOrderedEnumerable<Release>> GetAllReleases(string username, string repository)
|
||
1 year ago
|
{
|
||
|
var cacheKey = $"Releases-{username}-{repository}";
|
||
|
return memoryCache.GetOrCreateAsync(cacheKey, async entry =>
|
||
|
{
|
||
|
entry.SlidingExpiration = cacheDuration;
|
||
1 year ago
|
var allReleases = await githubApi.Repository.Release.GetAll(username, repository);
|
||
|
|
||
|
return allReleases.OrderByDescending(x => x.CreatedAt);
|
||
1 year ago
|
})!;
|
||
|
}
|
||
|
|
||
1 year ago
|
public Task<IReadOnlyList<Branch>> GetAllBranches(string username, string repository)
|
||
1 year ago
|
{
|
||
|
var cacheKey = $"Branches-{username}-{repository}";
|
||
|
return memoryCache.GetOrCreateAsync(cacheKey, async entry =>
|
||
|
{
|
||
|
entry.SlidingExpiration = cacheDuration;
|
||
1 year ago
|
var allReleases = await githubApi.Repository.Branch.GetAll(username, repository);
|
||
|
return allReleases;
|
||
1 year ago
|
})!;
|
||
|
}
|
||
|
|
||
1 year ago
|
public Task<IReadOnlyList<GitHubCommit>?> GetAllCommits(string username, string repository, string branch, int page = 1, int perPage = 10)
|
||
1 year ago
|
{
|
||
|
var cacheKey = $"Commits-{username}-{repository}-{branch}-{page}-{perPage}";
|
||
|
return memoryCache.GetOrCreateAsync(cacheKey, async entry =>
|
||
|
{
|
||
|
entry.SlidingExpiration = cacheDuration;
|
||
1 year ago
|
return await githubApi.Repository.Commit.GetAll(username, repository, new CommitRequest {Sha = branch},
|
||
|
new ApiOptions
|
||
|
{
|
||
|
PageCount = page,
|
||
|
PageSize = perPage,
|
||
|
StartPage = page
|
||
|
});
|
||
1 year ago
|
})!;
|
||
|
}
|
||
|
}
|