GarnetBuilderExtensions Methods
ClassMethods6 members
Provides extension methods for adding Garnet resources to the application model.
AddGarnet(IDistributedApplicationBuilder, string, int?)Section titled AddGarnet(IDistributedApplicationBuilder, string, int?)extensionIResourceBuilder<GarnetResource> Adds a Garnet container to the application model.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> AddGarnet( this IDistributedApplicationBuilder builder, string name, int? port) { // ... }}Parameters
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.portint?The host port to bind the underlying container to.Returns
IResourceBuilder<GarnetResource>A reference to the ApplicationModel.IResourceBuilder`1.Remarks
This version of the package defaults to the tag of the / container image. Use in application host Use in Api with Aspire.StackExchange.Redis
var builder = DistributedApplication.CreateBuilder(args);
var garnet = builder.AddGarnet("garnet");var api = builder.AddProject<Projects.Api>("api) .WithReference(garnet);
builder.Build().Run();var builder = WebApplication.CreateBuilder(args);builder.AddRedisClient("garnet");
var multiplexer = builder.Services.BuildServiceProvider() .GetRequiredService<IConnectionMultiplexer>();
var db = multiplexer.GetDatabase();db.HashSet("key", [new HashEntry("hash", "value")]);var value = db.HashGet("key", "hash");AddGarnet(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)Section titled AddGarnet(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<GarnetResource> Adds a Garnet container to the application model.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> AddGarnet( this IDistributedApplicationBuilder builder, string name, int? port = null, IResourceBuilder<ParameterResource>? password = null) { // ... }}Parameters
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.portint?optionalThe host port to bind the underlying container to.passwordIResourceBuilder<ParameterResource>optionalThe parameter used to provide the password for the Redis resource. If null a random password will be generated.Returns
IResourceBuilder<GarnetResource>A reference to the ApplicationModel.IResourceBuilder`1.Remarks
This version of the package defaults to the tag of the / container image. Use in application host Use in Api with Aspire.StackExchange.Redis
var builder = DistributedApplication.CreateBuilder(args);
var garnet = builder.AddGarnet("garnet");var api = builder.AddProject<Projects.Api>("api) .WithReference(garnet);
builder.Build().Run();var builder = WebApplication.CreateBuilder(args);builder.AddRedisClient("garnet");
var multiplexer = builder.Services.BuildServiceProvider() .GetRequiredService<IConnectionMultiplexer>();
var db = multiplexer.GetDatabase();db.HashSet("key", [new HashEntry("hash", "value")]);var value = db.HashGet("key", "hash");WithDataBindMount(IResourceBuilder<GarnetResource>, string, bool)Section titled WithDataBindMount(IResourceBuilder<GarnetResource>, string, bool)extensionIResourceBuilder<GarnetResource> Adds a bind mount for the data folder to a Garnet container resource and enables Garnet persistence.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> WithDataBindMount( this IResourceBuilder<GarnetResource> builder, string source, bool isReadOnly = false) { // ... }}Parameters
builderIResourceBuilder<GarnetResource>The resource builder.sourcestringThe source directory on the host to mount into the container.isReadOnlybooloptional A flag that indicates if this is a read-only mount. Setting this to true will disable Garnet persistence. Defaults to false. Returns
IResourceBuilder<GarnetResource>The ApplicationModel.IResourceBuilder`1.Remarks
Use
GarnetBuilderExtensions.WithPersistence to adjust Garnet persistence configuration, e.g.: var garnet = builder.AddGarnet("garnet") .WithDataBindMount("mydata") .WithPersistence(TimeSpan.FromSeconds(10));WithDataVolume(IResourceBuilder<GarnetResource>, string?, bool)Section titled WithDataVolume(IResourceBuilder<GarnetResource>, string?, bool)extensionIResourceBuilder<GarnetResource> Adds a named volume for the data folder to a Garnet container resource and enables Garnet persistence.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> WithDataVolume( this IResourceBuilder<GarnetResource> builder, string? name = null, bool isReadOnly = false) { // ... }}Parameters
builderIResourceBuilder<GarnetResource>The resource builder.namestring?optionalThe name of the volume. Defaults to an auto-generated name based on the application and resource names.isReadOnlybooloptional A flag that indicates if this is a read-only volume. Setting this to true will disable Garnet persistence. Defaults to false. Returns
IResourceBuilder<GarnetResource>The ApplicationModel.IResourceBuilder`1.Remarks
Use
GarnetBuilderExtensions.WithPersistence to adjust Garnet persistence configuration, e.g.: var cache = builder.AddGarnet("cache") .WithDataVolume() .WithPersistence(TimeSpan.FromSeconds(10));WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)Section titled WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)extensionIResourceBuilder<GarnetResource> Configures a Garnet container resource for persistence.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> WithPersistence( this IResourceBuilder<GarnetResource> builder, TimeSpan? interval, long keysChangedThreshold) { // ... }}Parameters
builderIResourceBuilder<GarnetResource>The resource builder.intervalTimeSpan?The interval between snapshot exports. Defaults to 60 seconds.keysChangedThresholdlongThe number of key change operations required to trigger a snapshot at the interval. Defaults to 1.Returns
IResourceBuilder<GarnetResource>The ApplicationModel.IResourceBuilder`1.Examples
Use with or to persist Garnet data across sessions with custom persistence configuration, e.g.:
var cache = builder.AddGarnet("cache") .WithDataVolume() .WithPersistence(TimeSpan.FromSeconds(10));WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?)Section titled WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?)extensionIResourceBuilder<GarnetResource> Configures a Garnet container resource for persistence.
public static class GarnetBuilderExtensions{ public static IResourceBuilder<GarnetResource> WithPersistence( this IResourceBuilder<GarnetResource> builder, TimeSpan? interval = null) { // ... }}Parameters
builderIResourceBuilder<GarnetResource>The resource builder.intervalTimeSpan?optionalThe interval between snapshot exports. Defaults to 60 seconds.Returns
IResourceBuilder<GarnetResource>The ApplicationModel.IResourceBuilder`1.Remarks
Use with
GarnetBuilderExtensions.WithDataBindMount or GarnetBuilderExtensions.WithDataVolume to persist Garnet data across sessions with custom persistence configuration, e.g.: var cache = builder.AddGarnet("cache") .WithDataVolume() .WithPersistence(TimeSpan.FromSeconds(10));