-
Notifications
You must be signed in to change notification settings - Fork 753
Add STJ path for PluginCacheEntry #7421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
| using NuGet.Common; | ||
| using NuGet.Shared; | ||
|
|
||
| namespace NuGet.Protocol.Plugins | ||
| { | ||
|
|
@@ -18,17 +20,25 @@ namespace NuGet.Protocol.Plugins | |
| /// </summary> | ||
| public sealed class PluginCacheEntry | ||
| { | ||
| private readonly IEnvironmentVariableReader _environmentVariableReader; | ||
|
|
||
| /// <summary> | ||
| /// Create a plugin cache entry. | ||
| /// </summary> | ||
| /// <param name="rootCacheFolder">The root cache folder, normally /localappdata/nuget/plugins-cache</param> | ||
| /// <param name="pluginFilePath">The full plugin file path, which will be used to create a key for the folder created in the root folder itself </param> | ||
| /// <param name="requestKey">A unique request key for the operation claims. Ideally the packageSourceRepository value of the PluginRequestKey. Example https://protected.package.feed/index.json, or Source-Agnostic</param> | ||
| public PluginCacheEntry(string rootCacheFolder, string pluginFilePath, string requestKey) | ||
| : this(rootCacheFolder, pluginFilePath, requestKey, environmentVariableReader: null) | ||
| { | ||
| } | ||
|
|
||
| internal PluginCacheEntry(string rootCacheFolder, string pluginFilePath, string requestKey, IEnvironmentVariableReader environmentVariableReader) | ||
| { | ||
| RootFolder = Path.Combine(rootCacheFolder, CachingUtility.RemoveInvalidFileNameChars(CachingUtility.ComputeHash(pluginFilePath, addIdentifiableCharacters: false))); | ||
| CacheFileName = Path.Combine(RootFolder, CachingUtility.RemoveInvalidFileNameChars(CachingUtility.ComputeHash(requestKey, addIdentifiableCharacters: false)) + ".dat"); | ||
| NewCacheFileName = CacheFileName + "-new"; | ||
| _environmentVariableReader = environmentVariableReader; | ||
| } | ||
|
|
||
| internal TimeSpan MaxAge { get; set; } = TimeSpan.FromDays(30); | ||
|
|
@@ -61,11 +71,19 @@ public void LoadFromFile() | |
|
|
||
| private void ProcessContent(Stream content) | ||
| { | ||
| var serializer = new JsonSerializer(); | ||
| using (var sr = new StreamReader(content)) | ||
| using (var jsonTextReader = new JsonTextReader(sr)) | ||
| if (NuGetFeatureFlags.UseSystemTextJsonDeserializationFeatureSwitch | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is just internal, NuGet generated, nuget read, I think we may be able to remove the non STJ path.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am assuming your point is rather about cache file. But we still have a public API |
||
| || NuGetFeatureFlags.IsSystemTextJsonDeserializationEnabledByEnvironment(_environmentVariableReader)) | ||
| { | ||
| OperationClaims = serializer.Deserialize<IReadOnlyList<OperationClaim>>(jsonTextReader); | ||
| OperationClaims = System.Text.Json.JsonSerializer.Deserialize(content, PluginCacheJsonContext.Default.IReadOnlyListOperationClaim); | ||
| } | ||
| else | ||
| { | ||
| var serializer = new Newtonsoft.Json.JsonSerializer(); | ||
| using (var sr = new StreamReader(content)) | ||
| using (var jsonTextReader = new JsonTextReader(sr)) | ||
| { | ||
| OperationClaims = serializer.Deserialize<IReadOnlyList<OperationClaim>>(jsonTextReader); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -90,7 +108,17 @@ public async Task UpdateCacheFileAsync() | |
| FileShare.None, | ||
| CachingUtility.BufferSize)) | ||
| { | ||
| var json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(OperationClaims, Formatting.Indented)); | ||
| byte[] json; | ||
| if (NuGetFeatureFlags.UseSystemTextJsonDeserializationFeatureSwitch | ||
| || NuGetFeatureFlags.IsSystemTextJsonDeserializationEnabledByEnvironment(_environmentVariableReader)) | ||
| { | ||
| json = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(OperationClaims, PluginCacheJsonContext.Default.IReadOnlyListOperationClaim); | ||
| } | ||
| else | ||
| { | ||
| json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(OperationClaims, Formatting.Indented)); | ||
| } | ||
|
|
||
| await fileStream.WriteAsync(json, 0, json.Length); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NuGet.Protocol.Plugins | ||
| { | ||
| [JsonSerializable(typeof(IReadOnlyList<OperationClaim>))] | ||
| [JsonSourceGenerationOptions(WriteIndented = true)] | ||
| internal partial class PluginCacheJsonContext : JsonSerializerContext | ||
| { | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.