Skip to content

Commit

Permalink
Add upgrade check cache to improve the CLI start up speed and reduce …
Browse files Browse the repository at this point in the history
…the # of calls to github
  • Loading branch information
vgmello committed Nov 14, 2023
1 parent 1a8e805 commit 896b9cb
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public async Task TryUpgradeApp()
{
try
{
if (!ShouldCheckForUpgrade())
return;

var currentAppVersion = _appMetadata.GetAppVersion();

if (currentAppVersion is null)
Expand Down Expand Up @@ -167,4 +170,33 @@ private static HttpClient CreateHttpClient()

return httpClient;
}

private static bool ShouldCheckForUpgrade()
{
var upgradeCheck = AppDataDirectory.GetPath("version_check.txt");

var lastUpgradeCheckDateUtc = ReadLastUpgradeCheckDate();

if (lastUpgradeCheckDateUtc is null || lastUpgradeCheckDateUtc < DateTime.UtcNow.AddDays(-1))
{
File.WriteAllText(upgradeCheck, DateTime.UtcNow.ToBinary().ToString());

return true;
}

return false;

DateTime? ReadLastUpgradeCheckDate()
{
if (!File.Exists(upgradeCheck))
return null;

var lastUpgradeCheckValue = File.ReadAllText(upgradeCheck);

if (!long.TryParse(lastUpgradeCheckValue, out var lastUpgradeCheckBinaryDateUtc))
return null;

return DateTime.FromBinary(lastUpgradeCheckBinaryDateUtc);
}
}
}

0 comments on commit 896b9cb

Please sign in to comment.