# GarnetBuilderExtensions Methods

- Package: [Aspire.Hosting.Garnet](/reference/api/csharp/aspire.hosting.garnet.md)
- Type: [GarnetBuilderExtensions](/reference/api/csharp/aspire.hosting.garnet/garnetbuilderextensions.md)
- Kind: `Methods`
- Members: `6`

Provides extension methods for adding Garnet resources to the application model.

## AddGarnet(IDistributedApplicationBuilder, string, int?)

- Name: `AddGarnet(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L59)

Adds a Garnet container to the application model.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> AddGarnet(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `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.

## 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

```csharp
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

```csharp
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");
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddGarnet(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)

- Name: `AddGarnet(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L104-L174)

Adds a Garnet container to the application model.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> AddGarnet(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port = null,
        IResourceBuilder<ParameterResource>? password = null)
    {
        // ...
    }
}
```

## Parameters

- `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.

## 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

```csharp
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

```csharp
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");
```

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## WithDataBindMount(IResourceBuilder<GarnetResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<GarnetResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L252-L261)

Adds a bind mount for the data folder to a Garnet container resource and enables Garnet persistence.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> WithDataBindMount(
        this IResourceBuilder<GarnetResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `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`.

## Returns

`IResourceBuilder<GarnetResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Use [GarnetBuilderExtensions.WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.garnet/garnetbuilderextensions/methods.md#withpersistence-iresourcebuilder-garnetresource-timespan-long) to adjust Garnet persistence configuration, e.g.:

```csharp
var garnet = builder.AddGarnet("garnet")
                   .WithDataBindMount("mydata")
                   .WithPersistence(TimeSpan.FromSeconds(10));
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<GarnetResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<GarnetResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L214-L223)

Adds a named volume for the data folder to a Garnet container resource and enables Garnet persistence.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> WithDataVolume(
        this IResourceBuilder<GarnetResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `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`.

## Returns

`IResourceBuilder<GarnetResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Use [GarnetBuilderExtensions.WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.garnet/garnetbuilderextensions/methods.md#withpersistence-iresourcebuilder-garnetresource-timespan-long) to adjust Garnet persistence configuration, e.g.:

```csharp
var cache = builder.AddGarnet("cache")
                   .WithDataVolume()
                   .WithPersistence(TimeSpan.FromSeconds(10));
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)

> **Obsolete:** This method is obsolete and will be removed in a future version. Use the overload without the keysChangedThreshold parameter.

- Name: `WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?, long)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L283)

Configures a Garnet container resource for persistence.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> WithPersistence(
        this IResourceBuilder<GarnetResource> builder,
        TimeSpan? interval,
        long keysChangedThreshold)
    {
        // ...
    }
}
```

## Parameters

- `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.

## Returns

`IResourceBuilder<GarnetResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Examples

Use with or to persist Garnet data across sessions with custom persistence configuration, e.g.:

```csharp
var cache = builder.AddGarnet("cache")
                   .WithDataVolume()
                   .WithPersistence(TimeSpan.FromSeconds(10));
```

## WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?)

- Name: `WithPersistence(IResourceBuilder<GarnetResource>, TimeSpan?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<GarnetResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs#L308-L310)

Configures a Garnet container resource for persistence.

```csharp
public static class GarnetBuilderExtensions
{
    public static IResourceBuilder<GarnetResource> WithPersistence(
        this IResourceBuilder<GarnetResource> builder,
        TimeSpan? interval = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<GarnetResource>`)
  The resource builder.
- `interval` (`TimeSpan?`) `optional`
  The interval between snapshot exports. Defaults to 60 seconds.

## Returns

`IResourceBuilder<GarnetResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Use with [GarnetBuilderExtensions.WithDataBindMount(IResourceBuilder<GarnetResource>, string, bool)](/reference/api/csharp/aspire.hosting.garnet/garnetbuilderextensions/methods.md#withdatabindmount-iresourcebuilder-garnetresource-string-bool) or [GarnetBuilderExtensions.WithDataVolume(IResourceBuilder<GarnetResource>, string?, bool)](/reference/api/csharp/aspire.hosting.garnet/garnetbuilderextensions/methods.md#withdatavolume-iresourcebuilder-garnetresource-string-bool) to persist Garnet data across sessions with custom persistence configuration, e.g.:

```csharp
var cache = builder.AddGarnet("cache")
                   .WithDataVolume()
                   .WithPersistence(TimeSpan.FromSeconds(10));
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
