Skip to content

Commit 579b89e

Browse files
authored
Migrate HybridCache from aspnetcore to extensions (#5391)
this is a direct snapshot from dotnet/aspnetcore, with modifications to: 1. satisfy local repo style rules 2. work with different build setup Minor global changes: 1. support netstandard2.1 on some legacy shim includes 2. add some additional required packages (for testing)
1 parent e19702b commit 579b89e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4371
-5
lines changed

Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<NoWarn>$(NoWarn);AD0001</NoWarn>
2323

2424
<!-- Experimental warnings are for customers, not for this repo -->
25-
<NoWarn>$(NoWarn);EXTEXP0001;EXTEXP0002;EXTEXP0003;EXTEXP0004;EXTEXP0005;EXTEXP0006;EXTEXP0007;EXTEXP0008;EXTEXP0009;EXTEXP0010;EXTEXP0011;EXTEXP0012;EXTEXP0013;EXTEXP0014;EXTEXP0015;EXTEXP0016;EXTEXP0017</NoWarn>
25+
<NoWarn>$(NoWarn);EXTEXP0001;EXTEXP0002;EXTEXP0003;EXTEXP0004;EXTEXP0005;EXTEXP0006;EXTEXP0007;EXTEXP0008;EXTEXP0009;EXTEXP0010;EXTEXP0011;EXTEXP0012;EXTEXP0013;EXTEXP0014;EXTEXP0015;EXTEXP0016;EXTEXP0017;EXTEXP0018</NoWarn>
2626

2727
<!-- Obsoletion warnings are for customers, not for this repo -->
2828
<NoWarn>$(NoWarn);EXTOBS0001;</NoWarn>

docs/list-of-diagnostics.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ if desired.
4040
| `EXTEXP0015` | Environmental probes experiments |
4141
| `EXTEXP0016` | Hosting integration testing experiments |
4242
| `EXTEXP0017` | Contextual options experiments |
43+
| `EXTEXP0018` | HybridCache experiments |
4344

4445
# Obsoletions
4546

@@ -81,7 +82,7 @@ You may continue using obsolete APIs in your application, but we advise explorin
8182
| `LOGGEN023` | Tag provider method is inaccessible |
8283
| `LOGGEN024` | Property provider method has an invalid signature |
8384
| `LOGGEN025` | Logging method parameters can't have "ref" or "out" modifiers |
84-
| `LOGGEN026` | Parameters with a custom tag provider are not subject to redaciton |
85+
| `LOGGEN026` | Parameters with a custom tag provider are not subject to redaction |
8586
| `LOGGEN027` | Multiple logging methods shouldn't use the same event name |
8687
| `LOGGEN028` | Logging method parameter's type has a hidden property |
8788
| `LOGGEN029` | A logging method parameter causes name conflicts |

eng/MSBuild/LegacySupport.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\IsExternalInit\*.cs" LinkBase="LegacySupport\IsExternalInit" />
88
</ItemGroup>
99

10-
<ItemGroup Condition="'$(InjectTrimAttributesOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp3.1')">
10+
<ItemGroup Condition="'$(InjectTrimAttributesOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netcoreapp3.1')">
1111
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\TrimAttributes\*.cs" LinkBase="LegacySupport\TrimAttributes" />
1212
</ItemGroup>
1313

14-
<ItemGroup Condition="'$(InjectCallerAttributesOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0')">
14+
<ItemGroup Condition="'$(InjectCallerAttributesOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1')">
1515
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\CallerAttributes\*.cs" LinkBase="LegacySupport\CallerAttributes" />
1616
</ItemGroup>
1717

@@ -47,7 +47,7 @@
4747
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\TaskWaitAsync\*.cs" LinkBase="LegacySupport\TaskWaitAsync" />
4848
</ItemGroup>
4949

50-
<ItemGroup Condition="'$(InjectExperimentalAttributeOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0')">
50+
<ItemGroup Condition="'$(InjectExperimentalAttributeOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0')">
5151
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\ExperimentalAttribute\*.cs" LinkBase="LegacySupport\ExperimentalAttribute" />
5252
</ItemGroup>
5353

eng/Version.Details.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<Uri>https://github.com/dotnet/runtime</Uri>
99
<Sha>31528d082bd760377b8d818fc839a338cd071b1f</Sha>
1010
</Dependency>
11+
<Dependency Name="Microsoft.Extensions.Caching.Memory" Version="9.0.0-rc.2.24453.5" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
12+
<Uri>https://github.com/dotnet/runtime</Uri>
13+
<Sha>418c3b9e2753715fa017ace6b3f1f5ec4d4d6aae</Sha>
14+
</Dependency>
1115
<Dependency Name="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0-rc.2.24453.5" CoherentParentDependency="Microsoft.AspNetCore.App.Runtime.win-x64">
1216
<Uri>https://github.com/dotnet/runtime</Uri>
1317
<Sha>31528d082bd760377b8d818fc839a338cd071b1f</Sha>
@@ -144,6 +148,10 @@
144148
<Uri>https://github.com/dotnet/aspnetcore</Uri>
145149
<Sha>fc4f8810d8df45a9f42e02f688041bf592c18138</Sha>
146150
</Dependency>
151+
<Dependency Name="Microsoft.Extensions.Caching.SqlServer" Version="9.0.0-rc.2.24460.5">
152+
<Uri>https://github.com/dotnet/aspnetcore</Uri>
153+
<Sha>2b865e33f2c7c9484c28a3b62e8ff07966e23434</Sha>
154+
</Dependency>
147155
<Dependency Name="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0-rc.2.24460.5">
148156
<Uri>https://github.com/dotnet/aspnetcore</Uri>
149157
<Sha>fc4f8810d8df45a9f42e02f688041bf592c18138</Sha>

eng/Versions.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<!-- Packages from dotnet/runtime -->
3131
<MicrosoftBclTimeProviderVersion>9.0.0-rc.2.24453.5</MicrosoftBclTimeProviderVersion>
3232
<MicrosoftExtensionsCachingAbstractionsVersion>9.0.0-rc.2.24453.5</MicrosoftExtensionsCachingAbstractionsVersion>
33+
<MicrosoftExtensionsCachingMemoryVersion>9.0.0-rc.2.24453.5</MicrosoftExtensionsCachingMemoryVersion>
3334
<MicrosoftExtensionsConfigurationAbstractionsVersion>9.0.0-rc.2.24453.5</MicrosoftExtensionsConfigurationAbstractionsVersion>
3435
<MicrosoftExtensionsConfigurationBinderVersion>9.0.0-rc.2.24453.5</MicrosoftExtensionsConfigurationBinderVersion>
3536
<MicrosoftExtensionsConfigurationJsonVersion>9.0.0-rc.2.24453.5</MicrosoftExtensionsConfigurationJsonVersion>
@@ -64,6 +65,7 @@
6465
<MicrosoftAspNetCoreAppRuntimewinx64Version>9.0.0-rc.2.24460.5</MicrosoftAspNetCoreAppRuntimewinx64Version>
6566
<MicrosoftAspNetCoreMvcTestingVersion>9.0.0-rc.2.24460.5</MicrosoftAspNetCoreMvcTestingVersion>
6667
<MicrosoftAspNetCoreTestHostVersion>9.0.0-rc.2.24460.5</MicrosoftAspNetCoreTestHostVersion>
68+
<MicrosoftExtensionsCachingSqlServerVersion>9.0.0-rc.2.24460.5</MicrosoftExtensionsCachingSqlServerVersion>
6769
<MicrosoftExtensionsCachingStackExchangeRedisVersion>9.0.0-rc.2.24460.5</MicrosoftExtensionsCachingStackExchangeRedisVersion>
6870
<MicrosoftExtensionsDiagnosticsHealthChecksVersion>9.0.0-rc.2.24460.5</MicrosoftExtensionsDiagnosticsHealthChecksVersion>
6971
<MicrosoftExtensionsHttpPollyVersion>9.0.0-rc.2.24460.5</MicrosoftExtensionsHttpPollyVersion>

eng/packages/General.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" />
1111
<PackageVersion Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
1212
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="$(MicrosoftExtensionsCachingAbstractionsVersion)" />
13+
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="$(MicrosoftExtensionsCachingMemoryVersion)" />
14+
<PackageVersion Include="Microsoft.Extensions.Caching.SqlServer" Version="$(MicrosoftExtensionsCachingSqlServerVersion)" />
15+
<PackageVersion Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="$(MicrosoftExtensionsCachingStackExchangeRedisVersion)" />
1316
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="$(MicrosoftExtensionsConfigurationAbstractionsVersion)" />
1417
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsConfigurationBinderVersion)" />
1518
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationJsonVersion)" />

eng/spellchecking_exclusions.dic

150 Bytes
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
using Microsoft.Extensions.Caching.Hybrid;
6+
using Microsoft.Shared.Diagnostics;
7+
8+
namespace Microsoft.Extensions.DependencyInjection;
9+
10+
/// <summary>
11+
/// Configuration extension methods for <see cref="IHybridCacheBuilder"/> / <see cref="HybridCache"/>.
12+
/// </summary>
13+
public static class HybridCacheBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Serialize values of type <typeparamref name="T"/> with the specified serializer from <paramref name="serializer"/>.
17+
/// </summary>
18+
/// <typeparam name="T">The type to be serialized.</typeparam>
19+
/// <returns>The <see cref="IHybridCacheBuilder"/> instance.</returns>
20+
public static IHybridCacheBuilder AddSerializer<T>(this IHybridCacheBuilder builder, IHybridCacheSerializer<T> serializer)
21+
{
22+
_ = Throw.IfNull(builder).Services.AddSingleton<IHybridCacheSerializer<T>>(serializer);
23+
return builder;
24+
}
25+
26+
/// <summary>
27+
/// Serialize values of type <typeparamref name="T"/> with the serializer of type <typeparamref name="TImplementation"/>.
28+
/// </summary>
29+
/// <typeparam name="T">The type to be serialized.</typeparam>
30+
/// <typeparam name="TImplementation">The serializer to use for this type.</typeparam>
31+
/// <returns>The <see cref="IHybridCacheBuilder"/> instance.</returns>
32+
public static IHybridCacheBuilder AddSerializer<T,
33+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(this IHybridCacheBuilder builder)
34+
where TImplementation : class, IHybridCacheSerializer<T>
35+
{
36+
_ = Throw.IfNull(builder).Services.AddSingleton<IHybridCacheSerializer<T>, TImplementation>();
37+
return builder;
38+
}
39+
40+
/// <summary>
41+
/// Add <paramref name="factory"/> as an additional serializer factory, which can provide serializers for multiple types.
42+
/// </summary>
43+
/// <returns>The <see cref="IHybridCacheBuilder"/> instance.</returns>
44+
public static IHybridCacheBuilder AddSerializerFactory(this IHybridCacheBuilder builder, IHybridCacheSerializerFactory factory)
45+
{
46+
_ = Throw.IfNull(builder).Services.AddSingleton<IHybridCacheSerializerFactory>(factory);
47+
return builder;
48+
}
49+
50+
/// <summary>
51+
/// Add a factory of type <typeparamref name="TImplementation"/> as an additional serializer factory, which can provide serializers for multiple types.
52+
/// </summary>
53+
/// <typeparam name="TImplementation">The type of the serializer factory.</typeparam>
54+
/// <returns>The <see cref="IHybridCacheBuilder"/> instance.</returns>
55+
public static IHybridCacheBuilder AddSerializerFactory<
56+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(this IHybridCacheBuilder builder)
57+
where TImplementation : class, IHybridCacheSerializerFactory
58+
{
59+
_ = Throw.IfNull(builder).Services.AddSingleton<IHybridCacheSerializerFactory, TImplementation>();
60+
return builder;
61+
}
62+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Extensions.Caching.Hybrid;
5+
6+
/// <summary>
7+
/// Options for configuring the default <see cref="HybridCache"/> implementation.
8+
/// </summary>
9+
public class HybridCacheOptions
10+
{
11+
private const int ShiftBytesToMibiBytes = 20;
12+
13+
/// <summary>
14+
/// Gets or sets the default global options to be applied to <see cref="HybridCache"/> operations; if options are
15+
/// specified at the individual call level, the non-null values are merged (with the per-call
16+
/// options being used in preference to the global options). If no value is specified for a given
17+
/// option (globally or per-call), the implementation may choose a reasonable default.
18+
/// </summary>
19+
public HybridCacheEntryOptions? DefaultEntryOptions { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets a value indicating whether compression for this <see cref="HybridCache"/> instance is disabled.
23+
/// </summary>
24+
public bool DisableCompression { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the maximum size of cache items; attempts to store values over this size will be logged
28+
/// and the value will not be stored in cache.
29+
/// </summary>
30+
/// <remarks>The default value is 1 MiB.</remarks>
31+
public long MaximumPayloadBytes { get; set; } = 1 << ShiftBytesToMibiBytes; // 1MiB
32+
33+
/// <summary>
34+
/// Gets or sets the maximum permitted length (in characters) of keys; attempts to use keys over this size will be logged.
35+
/// </summary>
36+
/// <remark>The default value is 1024 characters.</remark>
37+
public int MaximumKeyLength { get; set; } = 1024; // characters
38+
39+
/// <summary>
40+
/// Gets or sets a value indicating whether to use "tags" data as dimensions on metric reporting; if enabled, care should be used to ensure that
41+
/// tags do not contain data that should not be visible in metrics systems.
42+
/// </summary>
43+
public bool ReportTagMetrics { get; set; }
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.Extensions.Caching.Hybrid;
6+
using Microsoft.Extensions.Caching.Hybrid.Internal;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
using Microsoft.Shared.Diagnostics;
9+
10+
namespace Microsoft.Extensions.DependencyInjection;
11+
12+
/// <summary>
13+
/// Configuration extension methods for <see cref="HybridCache"/>.
14+
/// </summary>
15+
public static class HybridCacheServiceExtensions
16+
{
17+
/// <summary>
18+
/// Adds support for multi-tier caching services.
19+
/// </summary>
20+
/// <returns>A builder instance that allows further configuration of the <see cref="HybridCache"/> system.</returns>
21+
public static IHybridCacheBuilder AddHybridCache(this IServiceCollection services, Action<HybridCacheOptions> setupAction)
22+
{
23+
_ = Throw.IfNull(setupAction);
24+
_ = AddHybridCache(services);
25+
_ = services.Configure(setupAction);
26+
return new HybridCacheBuilder(services);
27+
}
28+
29+
/// <summary>
30+
/// Adds support for multi-tier caching services.
31+
/// </summary>
32+
/// <returns>A builder instance that allows further configuration of the <see cref="HybridCache"/> system.</returns>
33+
public static IHybridCacheBuilder AddHybridCache(this IServiceCollection services)
34+
{
35+
_ = Throw.IfNull(services);
36+
services.TryAddSingleton(TimeProvider.System);
37+
_ = services.AddOptions().AddMemoryCache();
38+
services.TryAddSingleton<IHybridCacheSerializerFactory, DefaultJsonSerializerFactory>();
39+
services.TryAddSingleton<IHybridCacheSerializer<string>>(InbuiltTypeSerializer.Instance);
40+
services.TryAddSingleton<IHybridCacheSerializer<byte[]>>(InbuiltTypeSerializer.Instance);
41+
services.TryAddSingleton<HybridCache, DefaultHybridCache>();
42+
return new HybridCacheBuilder(services);
43+
}
44+
}

0 commit comments

Comments
 (0)