From 370aa8fc189c62ad773524e71aea72918f3a118c Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Wed, 21 Aug 2024 13:28:19 +0300 Subject: [PATCH] fix cache moment --- src/controllers/PackageContentController.cs | 5 ++--- src/controllers/SearchController.cs | 14 +++++++++----- src/controllers/StateController.cs | 2 +- src/logic/entities/Package.cs | 5 +++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/controllers/PackageContentController.cs b/src/controllers/PackageContentController.cs index 9777f46..a8f8035 100644 --- a/src/controllers/PackageContentController.cs +++ b/src/controllers/PackageContentController.cs @@ -24,7 +24,7 @@ public async Task> GetPackageVersionsAsync if (versions == null) return NotFound(); - cache.Set($"@/packages/{id}/version.json", versions, TimeSpan.FromMinutes(15)); + cache.Set($"@/packages/{id}/version.json", versions, TimeSpan.FromMinutes(5)); return Json(versions); } @@ -84,8 +84,7 @@ public async Task DownloadReadmeAsync(string id, string version, var result = await reader.ReadToEndAsync(cancellationToken); - cache.Set($"@/packages/{id}/{version}/readme", result, - ver.HasMetadata ? TimeSpan.FromMinutes(15) : TimeSpan.FromHours(6)); + cache.Set($"@/packages/{id}/{version}/readme", result, TimeSpan.FromHours(6)); return Content(markdownService.GetHtmlFromMarkdown(result).Content, "text/html"); } diff --git a/src/controllers/SearchController.cs b/src/controllers/SearchController.cs index 02ff170..a7a76d0 100644 --- a/src/controllers/SearchController.cs +++ b/src/controllers/SearchController.cs @@ -20,13 +20,16 @@ public class SearchController( [HttpGet("@/package/{name}/{version}")] public async Task> FindByName(string name, string version, [FromQuery] bool includeUnlisted = false) { - if (cache.TryGetValue((name, version), out Package package)) + if (string.IsNullOrEmpty(version)) + return BadRequest(new { message = "version cannot be null" }); + + if (cache.TryGetValue((name, version), out Package? package)) return Json(package); var ver = version switch { - "latest" or null => new (0, 0, 0, 0, "", "latest"), - "next" => new (0, 0, 0, 0, "", "next"), + Package.LatestTag or null => new (0, 0, 0, 0, "", Package.LatestTag), + Package.NextTag => new (0, 0, 0, 0, "", Package.NextTag), not null => NuGetVersion.Parse(version) }; @@ -39,9 +42,10 @@ public async Task> FindByName(string name, string version, ? urlGenerator.GetPackageIconDownloadUrl(result.Name, result.Version) : result.Icon; + if (!version!.Equals(Package.LatestTag) && !version.Equals(Package.NextTag)) + return Json(result); - cache.Set((name, version), result, ver.HasMetadata ? TimeSpan.FromMinutes(15) : TimeSpan.FromDays(2)); - + cache.Set((name, version), result, TimeSpan.FromDays(2)); return Json(result); } diff --git a/src/controllers/StateController.cs b/src/controllers/StateController.cs index 0fbe699..f64a3cc 100644 --- a/src/controllers/StateController.cs +++ b/src/controllers/StateController.cs @@ -30,7 +30,7 @@ public async Task GetState() popular_packages = popular }; - cache.Set("@/state", result, TimeSpan.FromHours(12)); + cache.Set("@/state", result, TimeSpan.FromHours(24)); return Json(result); } diff --git a/src/logic/entities/Package.cs b/src/logic/entities/Package.cs index 38d3f4a..a63b118 100644 --- a/src/logic/entities/Package.cs +++ b/src/logic/entities/Package.cs @@ -30,4 +30,9 @@ public record Package : PackageManifest [JsonProperty("hasServicedPackage")] public bool HasServicedPackage { get; set; } + + + public const string LatestTag = "latest"; + public const string NextTag = "next"; + }