# ValkeyBuilderExtensions Methods

- Package: [Aspire.Hosting.Valkey](/reference/api/csharp/aspire.hosting.valkey.md)
- Type: [ValkeyBuilderExtensions](/reference/api/csharp/aspire.hosting.valkey/valkeybuilderextensions.md)
- Kind: `Methods`
- Members: `5`

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

## AddValkey(IDistributedApplicationBuilder, string, int?)

- Name: `AddValkey(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ValkeyResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs#L65-L68)

Adds a Valkey container to the application model.

```csharp
public static class ValkeyBuilderExtensions
{
    public static IResourceBuilder<ValkeyResource> AddValkey(
        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<ValkeyResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

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

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

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

- Name: `AddValkey(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ValkeyResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs#L118-L184)

Adds a Valkey container to the application model.

```csharp
public static class ValkeyBuilderExtensions
{
    public static IResourceBuilder<ValkeyResource> AddValkey(
        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 Valkey resource. If `null` a random password will be generated.

## Returns

`IResourceBuilder<ValkeyResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

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

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

- Name: `WithDataBindMount(IResourceBuilder<ValkeyResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ValkeyResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs#L255-L264)

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

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

## Parameters

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

## Returns

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

## Remarks

Use [ValkeyBuilderExtensions.WithPersistence(IResourceBuilder<ValkeyResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.valkey/valkeybuilderextensions/methods.md#withpersistence-iresourcebuilder-valkeyresource-timespan-long) to adjust Valkey persistence configuration, e.g.:

```csharp
var valkey = builder.AddValkey("valkey")
                   .WithDataBindMount("mydata")
                   .WithPersistence(TimeSpan.FromSeconds(10), 5);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

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

- Name: `WithDataVolume(IResourceBuilder<ValkeyResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ValkeyResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs#L215-L224)

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

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

## Parameters

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

## Returns

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

## Remarks

Use [ValkeyBuilderExtensions.WithPersistence(IResourceBuilder<ValkeyResource>, TimeSpan?, long)](/reference/api/csharp/aspire.hosting.valkey/valkeybuilderextensions/methods.md#withpersistence-iresourcebuilder-valkeyresource-timespan-long) to adjust Valkey persistence configuration, e.g.:

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

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

- Name: `WithPersistence(IResourceBuilder<ValkeyResource>, TimeSpan?, long)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<ValkeyResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs#L293-L296)

Configures a Valkey container resource for persistence.

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

## Parameters

- `builder` (`IResourceBuilder<ValkeyResource>`)
  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<ValkeyResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

Use with [ValkeyBuilderExtensions.WithDataBindMount(IResourceBuilder<ValkeyResource>, string, bool)](/reference/api/csharp/aspire.hosting.valkey/valkeybuilderextensions/methods.md#withdatabindmount-iresourcebuilder-valkeyresource-string-bool) or [ValkeyBuilderExtensions.WithDataVolume(IResourceBuilder<ValkeyResource>, string?, bool)](/reference/api/csharp/aspire.hosting.valkey/valkeybuilderextensions/methods.md#withdatavolume-iresourcebuilder-valkeyresource-string-bool) to persist Valkey data across sessions with custom persistence configuration, e.g.:

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
