Skip to content
Docs Try Aspire

GarnetBuilderExtensions Methods

Class Methods 6 members
Provides extension methods for adding Garnet resources to the application model.
AddGarnet(IDistributedApplicationBuilder, string, int?) Section titled AddGarnet(IDistributedApplicationBuilder, string, int?) extension IResourceBuilder<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)
{
// ...
}
}
builder IDistributedApplicationBuilder The Hosting.IDistributedApplicationBuilder.
name string The name of the resource. This name will be used as the connection string name when referenced in a dependency.
port int? The host port to bind the underlying container to.
IResourceBuilder<GarnetResource> A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the / container image. Use in application host
var builder = DistributedApplication.CreateBuilder(args);
var garnet = builder.AddGarnet("garnet");
var api = builder.AddProject<Projects.Api>("api)
.WithReference(garnet);
builder.Build().Run();
Use in Api with Aspire.StackExchange.Redis
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>) extension IResourceBuilder<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)
{
// ...
}
}
builder IDistributedApplicationBuilder The Hosting.IDistributedApplicationBuilder.
name string The name of the resource. This name will be used as the connection string name when referenced in a dependency.
port int? optional The host port to bind the underlying container to.
password IResourceBuilder<ParameterResource> optional The parameter used to provide the password for the Redis resource. If null a random password will be generated.
IResourceBuilder<GarnetResource> A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the / container image. Use in application host
var builder = DistributedApplication.CreateBuilder(args);
var garnet = builder.AddGarnet("garnet");
var api = builder.AddProject<Projects.Api>("api)
.WithReference(garnet);
builder.Build().Run();
Use in Api with Aspire.StackExchange.Redis
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) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<GarnetResource> The resource builder.
source string The source directory on the host to mount into the container.
isReadOnly bool optional A flag that indicates if this is a read-only mount. Setting this to true will disable Garnet persistence. Defaults to false.
IResourceBuilder<GarnetResource> The ApplicationModel.IResourceBuilder`1.
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) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<GarnetResource> The resource builder.
name string? optional The name of the volume. Defaults to an auto-generated name based on the application and resource names.
isReadOnly bool optional A flag that indicates if this is a read-only volume. Setting this to true will disable Garnet persistence. Defaults to false.
IResourceBuilder<GarnetResource> The ApplicationModel.IResourceBuilder`1.
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) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<GarnetResource> The resource builder.
interval TimeSpan? The interval between snapshot exports. Defaults to 60 seconds.
keysChangedThreshold long The number of key change operations required to trigger a snapshot at the interval. Defaults to 1.
IResourceBuilder<GarnetResource> The ApplicationModel.IResourceBuilder`1.

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?) extension IResourceBuilder<GarnetResource>
Configures a Garnet container resource for persistence.
public static class GarnetBuilderExtensions
{
public static IResourceBuilder<GarnetResource> WithPersistence(
this IResourceBuilder<GarnetResource> builder,
TimeSpan? interval = null)
{
// ...
}
}
builder IResourceBuilder<GarnetResource> The resource builder.
interval TimeSpan? optional The interval between snapshot exports. Defaults to 60 seconds.
IResourceBuilder<GarnetResource> The ApplicationModel.IResourceBuilder`1.
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));