Skip to content

Commit

Permalink
fix cache moment
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Aug 21, 2024
1 parent 91e00a1 commit 370aa8f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/controllers/PackageContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<ActionResult<PackageVersionsResponse>> 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);
}
Expand Down Expand Up @@ -84,8 +84,7 @@ public async Task<IActionResult> 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");
}
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public class SearchController(
[HttpGet("@/package/{name}/{version}")]
public async Task<ActionResult<Package>> 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)
};

Expand All @@ -39,9 +42,10 @@ public async Task<ActionResult<Package>> 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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/StateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<IActionResult> GetState()
popular_packages = popular
};

cache.Set("@/state", result, TimeSpan.FromHours(12));
cache.Set("@/state", result, TimeSpan.FromHours(24));

return Json(result);
}
Expand Down
5 changes: 5 additions & 0 deletions src/logic/entities/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}

0 comments on commit 370aa8f

Please sign in to comment.