Skip to content

Commit f2ddd27

Browse files
authored
[ST] 6. Enable caching by default and remove the temporary list (#10755)
1 parent 5e0677a commit f2ddd27

5 files changed

Lines changed: 21 additions & 90 deletions

File tree

src/Catalog/Dnx/DnxCatalogCollector.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ protected override async Task<bool> FetchAsync(
8585
ReadCursor back,
8686
CancellationToken cancellationToken)
8787
{
88-
await DnxPackageVersionIndexCacheControl.LoadPackageIdsToIncludeAsync(_storageFactory.Create(), _logger, cancellationToken);
89-
9088
return await CatalogCommitUtilities.ProcessCatalogCommitsAsync(
9189
client,
9290
front,
@@ -158,7 +156,7 @@ await catalogEntries.ForEachAsync(_maxConcurrentCommitItemsWithinBatch, async ca
158156
cancellationToken);
159157
var areRequiredPropertiesPresent = await AreRequiredPropertiesPresentAsync(destinationStorage, destinationUri);
160158

161-
if (isNupkgSynchronized && areRequiredPropertiesPresent && isPackageInIndex && !DnxPackageVersionIndexCacheControl.PackageIdsToInclude.Contains(packageId))
159+
if (isNupkgSynchronized && areRequiredPropertiesPresent && isPackageInIndex)
162160
{
163161
_logger.LogInformation("No changes detected: {Id}/{Version}", packageId, normalizedPackageVersion);
164162

src/Catalog/Dnx/DnxConstants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static class DnxConstants
1818
{ StorageConstants.ContentType, ApplicationOctetStreamContentType }
1919
};
2020

21+
// Default Cache Control of Package Version Index (at Storage)
22+
public const string DefaultCacheControlOfPackageVersionIndex = "max-age=10";
23+
2124
// Cache Duration of Package Version Index (at CDN)
2225
public static readonly TimeSpan CacheDurationOfPackageVersionIndex = TimeSpan.FromSeconds(60);
2326

src/Catalog/Dnx/DnxPackageVersionIndexCacheControl.cs

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,28 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using System.Threading;
6-
using System.Threading.Tasks;
5+
using System.Linq;
76
using Microsoft.Extensions.Logging;
8-
using Newtonsoft.Json.Linq;
9-
using NuGet.Services.Metadata.Catalog.Persistence;
107

118
namespace NuGet.Services.Metadata.Catalog.Dnx
129
{
1310
public static class DnxPackageVersionIndexCacheControl
1411
{
15-
private const string DefaultCacheControlForPackageVersionIndex = "max-age=10";
16-
private const string BlobNameOfPackageIdsToInclude = "PackageIdsToIncludeForCachingPackageVersionIndex.json";
17-
18-
public static HashSet<string> PackageIdsToInclude = new HashSet<string>();
12+
private static readonly IList<string> PackageIdsToExclude = new List<string>() {
13+
"BaseTestPackage",
14+
}.Select(p => p.ToLowerInvariant()).ToList();
1915

2016
public static string GetCacheControl(string id, ILogger logger)
2117
{
22-
if (PackageIdsToInclude.Contains(id))
23-
{
24-
logger.LogInformation("Add caching to the package version index of Package Id: {id}.", id);
25-
26-
return DefaultCacheControlForPackageVersionIndex;
27-
}
28-
else
18+
var cacheControl = DnxConstants.DefaultCacheControlOfPackageVersionIndex;
19+
if (PackageIdsToExclude.Contains(id))
2920
{
30-
return Constants.NoStoreCacheControl;
21+
cacheControl = Constants.NoStoreCacheControl;
3122
}
32-
}
3323

34-
public static async Task LoadPackageIdsToIncludeAsync(IStorage storage, ILogger logger, CancellationToken cancellationToken)
35-
{
36-
if (!storage.Exists(BlobNameOfPackageIdsToInclude))
37-
{
38-
logger.LogInformation("{BlobName} does not exist, at {Address}.", BlobNameOfPackageIdsToInclude, storage.BaseAddress);
39-
40-
return;
41-
}
42-
43-
logger.LogInformation("Loading the list of Package Ids from {BlobName}, at {Address}.", BlobNameOfPackageIdsToInclude, storage.BaseAddress);
44-
45-
PackageIdsToInclude = new HashSet<string>();
46-
string jsonFile = await storage.LoadStringAsync(storage.ResolveUri(BlobNameOfPackageIdsToInclude), cancellationToken);
47-
if (jsonFile != null)
48-
{
49-
JObject obj = JObject.Parse(jsonFile);
50-
JArray ids = obj["ids"] as JArray;
51-
if (ids != null)
52-
{
53-
foreach (JToken id in ids)
54-
{
55-
PackageIdsToInclude.Add(id.ToString().ToLowerInvariant());
56-
}
57-
}
58-
}
24+
logger.LogInformation("Get cache control: {cacheControl} for the package version index of package Id: {id}.", cacheControl, id);
5925

60-
logger.LogInformation("Loaded the list of Package Ids (Count: {Count}) from {BlobName}, at {Address}.", PackageIdsToInclude.Count, BlobNameOfPackageIdsToInclude, storage.BaseAddress);
26+
return cacheControl;
6127
}
6228
}
6329
}

tests/CatalogTests/Dnx/DnxMakerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -27,7 +27,7 @@ public class DnxMakerTests
2727
private const string _expectedCacheControl = "max-age=120";
2828
private const string _expectedNuspecContentType = "text/xml";
2929
private const string _expectedPackageContentType = "application/octet-stream";
30-
private const string _expectedPackageVersionIndexJsonCacheControl = "no-store";
30+
private const string _expectedPackageVersionIndexJsonCacheControl = "max-age=10";
3131
private const string _expectedPackageVersionIndexJsonContentType = "application/json";
3232
private const string _packageId = "testid";
3333
private const string _nupkgData = "nupkg data";
@@ -877,4 +877,4 @@ protected override Task OnCopyAsync(
877877
}
878878
}
879879
}
880-
}
880+
}
Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Threading;
7-
using System.Threading.Tasks;
84
using Microsoft.Extensions.Logging;
95
using Moq;
10-
using NuGet.Services.Metadata.Catalog;
116
using NuGet.Services.Metadata.Catalog.Dnx;
12-
using NuGet.Services.Metadata.Catalog.Persistence;
137
using Xunit;
148

159
namespace CatalogTests.Dnx
1610
{
1711
public class DnxPackageVersionIndexCacheControlTests
1812
{
19-
[Fact]
20-
public async Task LoadPackageIdsToIncludeAsync_BlobDoesNotExist()
21-
{
22-
var storage = new Mock<IStorage>();
23-
storage.Setup(s => s.Exists(It.IsAny<string>())).Returns(false);
24-
25-
DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>();
26-
27-
await DnxPackageVersionIndexCacheControl.LoadPackageIdsToIncludeAsync(storage.Object, Mock.Of<ILogger>(), It.IsAny<CancellationToken>());
28-
29-
Assert.Empty(DnxPackageVersionIndexCacheControl.PackageIdsToInclude);
30-
}
31-
3213
[Theory]
33-
[InlineData("{\"ids\":[]}", 0)]
34-
[InlineData("{\"ids\":[\"PackageId1\",\"packageid1\"]}", 1)]
35-
[InlineData("{\"ids\":[\"PackageId1\",\"PackageId2\"]}", 2)]
36-
public async Task LoadPackageIdsToIncludeAsync_BlobExists(string json, int count)
14+
[InlineData("PackageId1", "max-age=10")]
15+
[InlineData("BaseTestPackage", "max-age=10")]
16+
[InlineData("basetestpackage", "no-store")]
17+
public void GetCacheControl(string packageId, string cacheControl)
3718
{
38-
var storage = new Mock<IStorage>();
39-
storage.Setup(s => s.Exists(It.IsAny<string>())).Returns(true);
40-
storage.Setup(x => x.LoadStringAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(json);
41-
42-
DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>();
43-
44-
await DnxPackageVersionIndexCacheControl.LoadPackageIdsToIncludeAsync(storage.Object, Mock.Of<ILogger>(), It.IsAny<CancellationToken>());
45-
46-
Assert.Equal(count, DnxPackageVersionIndexCacheControl.PackageIdsToInclude.Count);
47-
}
48-
49-
[Fact]
50-
public void GetCacheControl()
51-
{
52-
DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>() { "packageid1" };
53-
54-
Assert.Equal("max-age=10", DnxPackageVersionIndexCacheControl.GetCacheControl("packageid1", Mock.Of<ILogger>()));
55-
Assert.Equal(Constants.NoStoreCacheControl, DnxPackageVersionIndexCacheControl.GetCacheControl("packageid2", Mock.Of<ILogger>()));
19+
Assert.Equal(cacheControl, DnxPackageVersionIndexCacheControl.GetCacheControl(packageId, Mock.Of<ILogger>()));
5620
}
5721
}
5822
}

0 commit comments

Comments
 (0)