# RedisBuilderExtensions Methods

- Package: [Aspire.Hosting.Redis](/reference/api/csharp/aspire.hosting.redis.md)
- Type: [RedisBuilderExtensions](/reference/api/csharp/aspire.hosting.redis/redisbuilderextensions.md)
- Kind: `Methods`
- Members: `13`

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

## AddRedis(IDistributedApplicationBuilder, string, int?)

- Name: `AddRedis(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L41)

Adds a Redis container to the application model.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> AddRedis(
        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<RedisResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This resource includes built-in health checks. When this resource is referenced as a dependency using the `ResourceBuilderExtensions.WaitFor` extension method then the dependent resource will wait until the Redis resource is able to service requests.

This version of the package defaults to the tag of the container image.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

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

- Name: `AddRedis(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L70-L201)

Adds a Redis container to the application model.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> AddRedis(
        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<RedisResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This resource includes built-in health checks. When this resource is referenced as a dependency using the `ResourceBuilderExtensions.WaitFor` extension method then the dependent resource will wait until the Redis resource is able to service requests.

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

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

- Name: `WithDataBindMount(IResourceBuilder<RedisResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L479-L487)

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

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

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  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 Redis persistence. Defaults to `false`.

## Returns

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

## Remarks

Use [RedisBuilderExtensions.WithPersistence(IResourceBuilder<RedisResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.redis/redisbuilderextensions/methods.md#withpersistence-iresourcebuilder-redisresource-timespan-long) to adjust Redis persistence configuration, e.g.:

```csharp
var cache = builder.AddRedis("cache")
                   .WithDataBindMount("myredisdata")
                   .WithPersistence(TimeSpan.FromSeconds(10), 5);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<RedisInsightResource>, string)

- Name: `WithDataBindMount(IResourceBuilder<RedisInsightResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisInsightResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L549-L552)

Adds a bind mount for the data folder to a Redis Insight container resource.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisInsightResource> WithDataBindMount(
        this IResourceBuilder<RedisInsightResource> builder,
        string source)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisInsightResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

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

- Name: `WithDataVolume(IResourceBuilder<RedisResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L446-L453)

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

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

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  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 Redis persistence. Defaults to `false`.

## Returns

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

## Remarks

Use [RedisBuilderExtensions.WithPersistence(IResourceBuilder<RedisResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.redis/redisbuilderextensions/methods.md#withpersistence-iresourcebuilder-redisresource-timespan-long) to adjust Redis persistence configuration, e.g.:

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<RedisInsightResource>, string?)

- Name: `WithDataVolume(IResourceBuilder<RedisInsightResource>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisInsightResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L534-L536)

Adds a named volume for the data folder to a Redis Insight container resource.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisInsightResource> WithDataVolume(
        this IResourceBuilder<RedisInsightResource> builder,
        string? name = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisInsightResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<RedisCommanderResource>, int?)

- Name: `WithHostPort(IResourceBuilder<RedisCommanderResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisCommanderResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs)

Configures the host port that the Redis Commander resource is exposed on instead of using randomly assigned port.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisCommanderResource> WithHostPort(
        this IResourceBuilder<RedisCommanderResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisCommanderResource>`)
  The resource builder for Redis Commander.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

`IResourceBuilder<RedisCommanderResource>` -- The resource builder for RedisCommander.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<RedisInsightResource>, int?)

- Name: `WithHostPort(IResourceBuilder<RedisInsightResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisInsightResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs)

Configures the host port that the Redis Insight resource is exposed on instead of using randomly assigned port.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisInsightResource> WithHostPort(
        this IResourceBuilder<RedisInsightResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisInsightResource>`)
  The resource builder for Redis Insight.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

`IResourceBuilder<RedisInsightResource>` -- The resource builder for RedisInsight.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<RedisResource>, int?)

- Name: `WithHostPort(IResourceBuilder<RedisResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs)

Configures the host port that the Redis resource is exposed on instead of using randomly assigned port.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> WithHostPort(
        this IResourceBuilder<RedisResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  The resource builder.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPassword(IResourceBuilder<RedisResource>, IResourceBuilder<ParameterResource>)

- Name: `WithPassword(IResourceBuilder<RedisResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L565-L568)

Configures the password that the Redis resource is used.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> WithPassword(
        this IResourceBuilder<RedisResource> builder,
        IResourceBuilder<ParameterResource>? password)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  The resource builder.
- `password` (`IResourceBuilder<ParameterResource>`)
  The parameter used to provide the password for the Redis resource. If `null`, no password will be configured.

## Returns

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

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

- Name: `WithPersistence(IResourceBuilder<RedisResource>, TimeSpan?, long)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L511-L514)

Configures a Redis container resource for persistence.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> WithPersistence(
        this IResourceBuilder<RedisResource> builder,
        TimeSpan? interval = null,
        long keysChangedThreshold = 1)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  The resource builder.
- `interval` (`TimeSpan?`) `optional`
  The interval between snapshot exports. Defaults to 60 seconds.
- `keysChangedThreshold` (`long`) `optional`
  The number of key change operations required to trigger a snapshot at the interval. Defaults to 1.

## Returns

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

## Remarks

Use with [RedisBuilderExtensions.WithDataBindMount(IResourceBuilder<RedisResource>, string, bool)](/reference/api/csharp/aspire.hosting.redis/redisbuilderextensions/methods.md#withdatabindmount-iresourcebuilder-redisresource-string-bool) or [RedisBuilderExtensions.WithDataVolume(IResourceBuilder<RedisResource>, string?, bool)](/reference/api/csharp/aspire.hosting.redis/redisbuilderextensions/methods.md#withdatavolume-iresourcebuilder-redisresource-string-bool) to persist Redis data across sessions with custom persistence configuration, e.g.:

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRedisCommander(IResourceBuilder<RedisResource>, Action<IResourceBuilder<RedisCommanderResource>>, string?)

- Name: `WithRedisCommander(IResourceBuilder<RedisResource>, Action<IResourceBuilder<RedisCommanderResource>>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L218-L279)

Configures a container resource for Redis Commander which is pre-configured to connect to the [RedisResource](/reference/api/csharp/aspire.hosting.redis/redisresource.md) that this method is used on.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> WithRedisCommander(
        this IResourceBuilder<RedisResource> builder,
        Action<IResourceBuilder<RedisCommanderResource>>? configureContainer = null,
        string? containerName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  The `ApplicationModel.IResourceBuilder`1` for the [RedisResource](/reference/api/csharp/aspire.hosting.redis/redisresource.md).
- `configureContainer` (`Action<IResourceBuilder<RedisCommanderResource>>`) `optional`
  Configuration callback for Redis Commander container resource.
- `containerName` (`string?`) `optional`
  Override the container name used for Redis Commander.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithRedisInsight(IResourceBuilder<RedisResource>, Action<IResourceBuilder<RedisInsightResource>>, string?)

- Name: `WithRedisInsight(IResourceBuilder<RedisResource>, Action<IResourceBuilder<RedisInsightResource>>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<RedisResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs#L297-L385)

Configures a container resource for Redis Insight which is pre-configured to connect to the [RedisResource](/reference/api/csharp/aspire.hosting.redis/redisresource.md) that this method is used on.

```csharp
public static class RedisBuilderExtensions
{
    public static IResourceBuilder<RedisResource> WithRedisInsight(
        this IResourceBuilder<RedisResource> builder,
        Action<IResourceBuilder<RedisInsightResource>>? configureContainer = null,
        string? containerName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<RedisResource>`)
  The `ApplicationModel.IResourceBuilder`1` for the [RedisResource](/reference/api/csharp/aspire.hosting.redis/redisresource.md).
- `configureContainer` (`Action<IResourceBuilder<RedisInsightResource>>`) `optional`
  Configuration callback for Redis Insight container resource.
- `containerName` (`string?`) `optional`
  Override the container name used for Redis Insight.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
