Skip to content

Commit 603e850

Browse files
committed
Smarter mainfest caching on NuGet-based managers
1 parent da8c25a commit 603e850

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGet.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace UniGetUI.PackageEngine.Managers.PowerShellManager
1414
{
1515
public abstract class BaseNuGet : PackageManager
1616
{
17+
public static Dictionary<long, string> Manifests = new();
18+
1719
public sealed override void Initialize()
1820
{
1921
if (DetailsHelper is not BaseNuGetDetailsHelper)
@@ -39,6 +41,7 @@ private struct SearchResult
3941
public string version;
4042
public CoreTools.Version version_float;
4143
public string id;
44+
public string manifest;
4245
}
4346

4447
protected sealed override IReadOnlyList<Package> FindPackages_UnSafe(string query)
@@ -93,14 +96,13 @@ protected sealed override IReadOnlyList<Package> FindPackages_UnSafe(string quer
9396
var float_version = CoreTools.VersionStringToStruct(version);
9497
// Match title = Regex.Match(match.Value, "<title[ \\\"\\=A-Za-z0-9]+>([^<>]+)<\\/title>");
9598

96-
if (AlreadyProcessedPackages.TryGetValue(id, out var value) &&
97-
value.version_float >= float_version)
99+
if (AlreadyProcessedPackages.TryGetValue(id, out var value) && value.version_float >= float_version)
98100
{
99101
continue;
100102
}
101103

102104
AlreadyProcessedPackages[id] =
103-
new SearchResult { id = id, version = version, version_float = float_version };
105+
new SearchResult { id = id, version = version, version_float = float_version, manifest = match.Value };
104106
}
105107

106108
SearchUrl = null;
@@ -115,7 +117,9 @@ protected sealed override IReadOnlyList<Package> FindPackages_UnSafe(string quer
115117
foreach (SearchResult package in AlreadyProcessedPackages.Values)
116118
{
117119
logger.Log($"Found package {package.id} version {package.version} on source {source.Name}");
118-
Packages.Add(new Package(CoreTools.FormatAsName(package.id), package.id, package.version, source, this));
120+
var nativePackage = new Package(CoreTools.FormatAsName(package.id), package.id, package.version, source, this);
121+
Packages.Add(nativePackage);
122+
Manifests[nativePackage.GetHash()] = package.manifest;
119123
}
120124
}
121125

@@ -182,7 +186,10 @@ protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
182186
// Match title = Regex.Match(match.Value, "<title[ \\\"\\=A-Za-z0-9]+>([^<>]+)<\\/title>");
183187

184188
logger.Log($"Found package {id} version {new_version} on source {pair.Key.Name}");
185-
Packages.Add(new Package(CoreTools.FormatAsName(id), id, packageIdVersion[id.ToLower()], new_version, pair.Key, this));
189+
190+
var nativePackage = new Package(CoreTools.FormatAsName(id), id, packageIdVersion[id.ToLower()], new_version, pair.Key, this);
191+
Packages.Add(nativePackage);
192+
Manifests[nativePackage.GetHash()] = match.Value;
186193
}
187194
}
188195
}

src/UniGetUI.PackageEngine.Managers.Generic.NuGet/Internal/NuGetManifestLoader.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using UniGetUI.Core.Data;
22
using UniGetUI.Core.Logging;
33
using UniGetUI.PackageEngine.Interfaces;
4+
using UniGetUI.PackageEngine.Managers.PowerShellManager;
45

56
namespace UniGetUI.PackageEngine.Managers.Generic.NuGet.Internal
67
{
78
internal static class NuGetManifestLoader
89
{
9-
private static readonly Dictionary<string, string> __manifest_cache = [];
10-
1110
/// <summary>
1211
/// Returns the URL to the manifest of a NuGet-based package
1312
/// </summary>
@@ -35,14 +34,15 @@ public static Uri GetNuPkgUrl(IPackage package)
3534
/// <returns>A string containing the contents of the manifest</returns>
3635
public static string? GetManifestContent(IPackage package)
3736
{
38-
string? PackageManifestContent = "";
39-
string PackageManifestUrl = GetManifestUrl(package).ToString();
40-
if (__manifest_cache.TryGetValue(PackageManifestUrl, out var content))
37+
if (BaseNuGet.Manifests.TryGetValue(package.GetHash(), out string? manifest))
4138
{
4239
Logger.Debug($"Loading cached NuGet manifest for package {package.Id} on manager {package.Manager.Name}");
43-
return content;
40+
return manifest;
4441
}
4542

43+
string? PackageManifestContent = "";
44+
string PackageManifestUrl = GetManifestUrl(package).ToString();
45+
4646
try
4747
{
4848
using (HttpClient client = new(CoreData.GenericHttpClientParameters))
@@ -62,7 +62,7 @@ public static Uri GetNuPkgUrl(IPackage package)
6262

6363
PackageManifestContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
6464
}
65-
__manifest_cache[PackageManifestUrl] = PackageManifestContent;
65+
BaseNuGet.Manifests[package.GetHash()] = PackageManifestContent;
6666
return PackageManifestContent;
6767
}
6868
catch (Exception e)

0 commit comments

Comments
 (0)