-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in-memory cache for persisted queries. (#2872)
- Loading branch information
1 parent
687be58
commit 2131a7a
Showing
27 changed files
with
1,099 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...stem/Extensions/HotChocolateFileSystemPersistedQueriesRequestExecutorBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...Memory/Extensions/HotChocolateInMemoryPersistedQueriesRequestExecutorBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using HotChocolate; | ||
using HotChocolate.Execution.Configuration; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// Provides utility methods to setup dependency injection. | ||
/// </summary> | ||
public static class HotChocolateInMemoryPersistedQueriesRequestExecutorBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a file system read and write query storage to the | ||
/// services collection. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The service collection to which the services are added. | ||
/// </param> | ||
/// <param name="cacheDirectory"> | ||
/// The directory path that shall be used to store queries. | ||
/// </param> | ||
public static IRequestExecutorBuilder AddInMemoryQueryStorage( | ||
this IRequestExecutorBuilder builder) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return builder.ConfigureSchemaServices( | ||
s => s.AddInMemoryQueryStorage()); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a file system read-only query storage to the | ||
/// services collection. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The service collection to which the services are added. | ||
/// </param> | ||
/// <param name="cacheDirectory"> | ||
/// The directory path that shall be used to read queries from. | ||
/// </param> | ||
public static IRequestExecutorBuilder AddReadOnlyInMemoryQueryStorage( | ||
this IRequestExecutorBuilder builder) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return builder.ConfigureSchemaServices( | ||
s => s.AddReadOnlyInMemoryQueryStorage()); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...es.InMemory/Extensions/HotChocolateInMemoryPersistedQueriesServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.Execution; | ||
using HotChocolate.PersistedQueries.FileSystem; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace HotChocolate | ||
{ | ||
/// <summary> | ||
/// Provides utility methods to setup dependency injection. | ||
/// </summary> | ||
public static class HotChocolateInMemoryPersistedQueriesServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a file system read and write query storage to the | ||
/// services collection. | ||
/// </summary> | ||
/// <param name="services"> | ||
/// The service collection to which the services are added. | ||
/// </param> | ||
/// <param name="cacheDirectory"> | ||
/// The directory path that shall be used to store queries. | ||
/// </param> | ||
public static IServiceCollection AddInMemoryQueryStorage( | ||
this IServiceCollection services) | ||
{ | ||
if (services is null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
return services | ||
.AddReadOnlyInMemoryQueryStorage() | ||
.AddSingleton<IWriteStoredQueries>( | ||
sp => sp.GetRequiredService<InMemoryQueryStorage>()); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a file system read-only query storage to the | ||
/// services collection. | ||
/// </summary> | ||
/// <param name="services"> | ||
/// The service collection to which the services are added. | ||
/// </param> | ||
/// <param name="cacheDirectory"> | ||
/// The directory path that shall be used to read queries from. | ||
/// </param> | ||
public static IServiceCollection AddReadOnlyInMemoryQueryStorage( | ||
this IServiceCollection services) | ||
{ | ||
if (services is null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
return services | ||
.RemoveService<IReadStoredQueries>() | ||
.RemoveService<IWriteStoredQueries>() | ||
.AddSingleton(c => new InMemoryQueryStorage( | ||
c.GetService<IMemoryCache>() ?? | ||
c.GetApplicationService<IMemoryCache>())) | ||
.AddSingleton<IReadStoredQueries>( | ||
sp => sp.GetRequiredService<InMemoryQueryStorage>()); | ||
} | ||
|
||
private static IServiceCollection RemoveService<TService>( | ||
this IServiceCollection services) | ||
{ | ||
ServiceDescriptor? serviceDescriptor = | ||
services.FirstOrDefault(t => t.ServiceType == typeof(TService)); | ||
|
||
if (serviceDescriptor != null) | ||
{ | ||
services.Remove(serviceDescriptor); | ||
} | ||
|
||
return services; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...sistedQueries/src/PersistedQueries.InMemory/HotChocolate.PersistedQueries.InMemory.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current"> | ||
|
||
<PropertyGroup> | ||
<PackageId>HotChocolate.PersistedQueries.InMemory</PackageId> | ||
<AssemblyName>HotChocolate.PersistedQueries.InMemory</AssemblyName> | ||
<RootNamespace>HotChocolate.PersistedQueries.InMemory</RootNamespace> | ||
<Description>An implementation of Hot Chocolate persisted queries using Microsoft.Extensions.Caching.Memory.IMemoryCache.</Description> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="Properties\Resources.Designer.cs"> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>Resources.resx</DependentUpon> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Update="Properties\Resources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.