Skip to content

Commit b159d27

Browse files
Move cache configuration to options (#8793)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent bc35cef commit b159d27

27 files changed

+602
-413
lines changed
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using HotChocolate.Execution.Configuration;
22
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Options;
34

45
namespace HotChocolate.AspNetCore.Warmup;
56

67
internal sealed class RequestExecutorWarmupService(
7-
IRequestExecutorOptionsMonitor executorOptionsMonitor,
8+
IOptionsMonitor<RequestExecutorSetup> optionsMonitor,
89
IRequestExecutorProvider provider) : IHostedService
910
{
1011
public async Task StartAsync(CancellationToken cancellationToken)
@@ -13,8 +14,8 @@ public async Task StartAsync(CancellationToken cancellationToken)
1314

1415
foreach (var schemaName in provider.SchemaNames)
1516
{
16-
var setup = await executorOptionsMonitor.GetAsync(schemaName, cancellationToken);
17-
var options = CreateSchemaOptions(setup);
17+
var setup = optionsMonitor.Get(schemaName);
18+
var options = setup.CreateSchemaOptions();
1819

1920
if (!options.LazyInitialization)
2021
{
@@ -32,16 +33,4 @@ private async Task WarmupAsync(string schemaName, CancellationToken cancellation
3233
{
3334
await provider.GetExecutorAsync(schemaName, cancellationToken).ConfigureAwait(false);
3435
}
35-
36-
private static SchemaOptions CreateSchemaOptions(RequestExecutorSetup setup)
37-
{
38-
var options = new SchemaOptions();
39-
40-
foreach (var configure in setup.SchemaOptionModifiers)
41-
{
42-
configure(options);
43-
}
44-
45-
return options;
46-
}
4736
}

src/HotChocolate/Core/src/Execution/Caching/PreparedOperationCacheOptions.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/HotChocolate/Core/src/Execution/Configuration/DefaultRequestExecutorOptionsMonitor.cs

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/HotChocolate/Core/src/Execution/Configuration/IRequestExecutorOptionsMonitor.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/HotChocolate/Core/src/Execution/Configuration/IRequestExecutorOptionsProvider.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/HotChocolate/Core/src/Execution/Configuration/RequestExecutorSetup.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,16 @@ public void CopyTo(RequestExecutorSetup options)
134134
options.DefaultPipelineFactory = DefaultPipelineFactory;
135135
}
136136
}
137+
138+
internal SchemaOptions CreateSchemaOptions()
139+
{
140+
var options = new SchemaOptions();
141+
142+
foreach (var configure in SchemaOptionModifiers)
143+
{
144+
configure(options);
145+
}
146+
147+
return options;
148+
}
137149
}

src/HotChocolate/Core/src/Execution/DependencyInjection/InternalServiceCollectionExtensions.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ namespace Microsoft.Extensions.DependencyInjection;
2121

2222
internal static class InternalServiceCollectionExtensions
2323
{
24-
internal static IServiceCollection TryAddRequestExecutorFactoryOptionsMonitor(
25-
this IServiceCollection services)
26-
{
27-
services.TryAddSingleton<IRequestExecutorOptionsMonitor>(
28-
sp => new DefaultRequestExecutorOptionsMonitor(
29-
sp.GetRequiredService<IOptionsMonitor<RequestExecutorSetup>>(),
30-
sp.GetServices<IRequestExecutorOptionsProvider>()));
31-
return services;
32-
}
33-
3424
internal static IServiceCollection TryAddVariableCoercion(
3525
this IServiceCollection services)
3626
{
@@ -159,16 +149,6 @@ internal static IServiceCollection TryAddRequestExecutorResolver(
159149
return services;
160150
}
161151

162-
internal static IServiceCollection TryAddDefaultCaches(
163-
this IServiceCollection services)
164-
{
165-
services.TryAddSingleton(_ => new PreparedOperationCacheOptions { Capacity = 256 });
166-
services.TryAddSingleton<IDocumentCache>(
167-
sp => new DefaultDocumentCache(
168-
sp.GetRequiredService<PreparedOperationCacheOptions>().Capacity));
169-
return services;
170-
}
171-
172152
internal static IServiceCollection TryAddDefaultDocumentHashProvider(
173153
this IServiceCollection services)
174154
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using HotChocolate;
2+
using HotChocolate.Execution.Caching;
3+
using HotChocolate.Execution.Configuration;
4+
using HotChocolate.Language;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace Microsoft.Extensions.DependencyInjection;
9+
10+
public static partial class RequestExecutorBuilderExtensions
11+
{
12+
internal static IRequestExecutorBuilder AddDocumentCache(this IRequestExecutorBuilder builder)
13+
{
14+
builder.Services.TryAddKeyedSingleton<IDocumentCache>(
15+
builder.Name,
16+
static (sp, schemaName) =>
17+
{
18+
var optionsMonitor = sp.GetRequiredService<IOptionsMonitor<RequestExecutorSetup>>();
19+
var setup = optionsMonitor.Get((string)schemaName!);
20+
var options = setup.CreateSchemaOptions();
21+
22+
return new DefaultDocumentCache(options.OperationDocumentCacheSize);
23+
});
24+
25+
return builder.ConfigureSchemaServices(
26+
static (applicationServices, s) =>
27+
s.AddSingleton<IDocumentCache>(schemaServices =>
28+
{
29+
var schemaName = schemaServices.GetRequiredService<ISchemaDefinition>().Name;
30+
return applicationServices.GetRequiredKeyedService<IDocumentCache>(schemaName);
31+
}));
32+
}
33+
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorServiceCollectionExtensions.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ public static IServiceCollection AddGraphQLCore(this IServiceCollection services
4646

4747
// core services
4848
services
49-
.TryAddRequestExecutorFactoryOptionsMonitor()
5049
.TryAddTypeConverter()
5150
.TryAddInputFormatter()
5251
.TryAddInputParser()
53-
.TryAddDefaultCaches()
5452
.TryAddDefaultDocumentHashProvider()
5553
.TryAddDefaultBatchDispatcher()
5654
.TryAddDefaultDataLoaderRegistry()
@@ -156,6 +154,8 @@ private static DefaultRequestExecutorBuilder CreateBuilder(
156154
builder.TryAddTypeInterceptor<DataLoaderRootFieldTypeInterceptor>();
157155
builder.TryAddTypeInterceptor<RequirementsTypeInterceptor>();
158156

157+
builder.AddDocumentCache();
158+
159159
if (!services.Any(t =>
160160
t.ServiceType == typeof(SchemaName)
161161
&& t.ImplementationInstance is SchemaName s
@@ -169,25 +169,6 @@ private static DefaultRequestExecutorBuilder CreateBuilder(
169169
return builder;
170170
}
171171

172-
public static IServiceCollection AddDocumentCache(
173-
this IServiceCollection services,
174-
int capacity = 256)
175-
{
176-
services.RemoveAll<IDocumentCache>();
177-
services.AddSingleton<IDocumentCache>(
178-
_ => new DefaultDocumentCache(capacity));
179-
return services;
180-
}
181-
182-
public static IServiceCollection AddOperationCache(
183-
this IServiceCollection services,
184-
int capacity = 256)
185-
{
186-
services.RemoveAll<PreparedOperationCacheOptions>();
187-
services.AddSingleton(_ => new PreparedOperationCacheOptions { Capacity = capacity });
188-
return services;
189-
}
190-
191172
public static IServiceCollection AddMD5DocumentHashProvider(
192173
this IServiceCollection services,
193174
HashFormat format = HashFormat.Base64)

0 commit comments

Comments
 (0)