# AspireRedisDistributedCacheExtensions Methods

- Package: [Aspire.StackExchange.Redis.DistributedCaching](/reference/api/csharp/aspire.stackexchange.redis.distributedcaching.md)
- Type: [AspireRedisDistributedCacheExtensions](/reference/api/csharp/aspire.stackexchange.redis.distributedcaching/aspireredisdistributedcacheextensions.md)
- Kind: `Methods`
- Members: `4`

Provides extension methods for adding Redis distributed caching services to an `Hosting.IHostApplicationBuilder`.

## AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>)

- Name: `AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.StackExchange.Redis.DistributedCaching/AspireRedisDistributedCacheExtensions.cs#L67-L76)

Adds Redis distributed caching services, `Distributed.IDistributedCache`, in the services provided by the `builder`.

```csharp
public static class AspireRedisDistributedCacheExtensions
{
    public static void AddKeyedRedisDistributedCache(
        this IHostApplicationBuilder builder,
        string name,
        Action<StackExchangeRedisSettings>? configureSettings = null,
        Action<ConfigurationOptions>? configureOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<StackExchangeRedisSettings>`) `optional`
  An optional method that can be used for customizing the `Redis.StackExchangeRedisSettings`. It's invoked after the settings are read from the configuration.
- `configureOptions` (`Action<ConfigurationOptions>`) `optional`
  An optional method that can be used for customizing the `Redis.ConfigurationOptions`. It's invoked after the options are read from the configuration.

## Remarks

Reads the configuration from "Aspire:StackExchange:Redis:{name}" section. Also registers `Redis.IConnectionMultiplexer` as a singleton in the services provided by the `builder`. Enables retries, corresponding health check, logging, and telemetry.

## AddRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>)

- Name: `AddRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.StackExchange.Redis.DistributedCaching/AspireRedisDistributedCacheExtensions.cs#L37-L46)

Adds Redis distributed caching services, `Distributed.IDistributedCache`, in the services provided by the `builder`.

```csharp
public static class AspireRedisDistributedCacheExtensions
{
    public static void AddRedisDistributedCache(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<StackExchangeRedisSettings>? configureSettings = null,
        Action<ConfigurationOptions>? configureOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<StackExchangeRedisSettings>`) `optional`
  An optional method that can be used for customizing the `Redis.StackExchangeRedisSettings`. It's invoked after the settings are read from the configuration.
- `configureOptions` (`Action<ConfigurationOptions>`) `optional`
  An optional method that can be used for customizing the `Redis.ConfigurationOptions`. It's invoked after the options are read from the configuration.

## Remarks

Reads the configuration from "Aspire:StackExchange:Redis" section. Also registers `Redis.IConnectionMultiplexer` as a singleton in the services provided by the `builder`. Enables retries, corresponding health check, logging, and telemetry.

## WithDistributedCache(AspireRedisClientBuilder, Action<RedisCacheOptions>)

- Name: `WithDistributedCache(AspireRedisClientBuilder, Action<RedisCacheOptions>)`
- Modifiers: `extension`
- Returns: `AspireRedisClientBuilder`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.StackExchange.Redis.DistributedCaching/AspireRedisDistributedCacheExtensions.cs#L101-L118)

Configures the Redis client to also provide distributed caching services through `Distributed.IDistributedCache`.

```csharp
public static class AspireRedisDistributedCacheExtensions
{
    public static AspireRedisClientBuilder WithDistributedCache(
        this AspireRedisClientBuilder builder,
        Action<RedisCacheOptions>? configureOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`AspireRedisClientBuilder`)
  The `Redis.AspireRedisClientBuilder` to configure.
- `configureOptions` (`Action<RedisCacheOptions>`) `optional`
  An optional method that can be used for customizing the `StackExchangeRedis.RedisCacheOptions`.

## Returns

`AspireRedisClientBuilder` -- The `Redis.AspireRedisClientBuilder` for method chaining.

## Examples

The following example creates an IDistributedCache service using the Redis client connection named "redis". The created IDistributedCache service can then be resolved from an IServiceProvider: IServiceProvider serviceProvider = builder.Services.BuildServiceProvider(); var cache = serviceProvider.GetRequiredService<IDistributedCache>();

```csharp
var builder = WebApplication.CreateBuilder(args);

builder.AddRedisClientBuilder("redis")
       .WithDistributedCache();
```

## WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>)

- Name: `WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>)`
- Modifiers: `extension`
- Returns: `AspireRedisClientBuilder`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.StackExchange.Redis.DistributedCaching/AspireRedisDistributedCacheExtensions.cs#L153-L177)

Configures the Redis client to provide a keyed distributed caching service through `Distributed.IDistributedCache` using `name` as the key.

```csharp
public static class AspireRedisDistributedCacheExtensions
{
    public static AspireRedisClientBuilder WithKeyedDistributedCache(
        this AspireRedisClientBuilder builder,
        string name,
        Action<RedisCacheOptions>? configureOptions = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`AspireRedisClientBuilder`)
  The `Redis.AspireRedisClientBuilder` to configure.
- `name` (`string`)
  The name which is used as the `ServiceDescriptor.ServiceKey` of the service.
- `configureOptions` (`Action<RedisCacheOptions>`) `optional`
  An optional method that can be used for customizing the `StackExchangeRedis.RedisCacheOptions`.

## Returns

`AspireRedisClientBuilder` -- The `Redis.AspireRedisClientBuilder` for method chaining.

## Examples

The following example creates keyed IDistributedCache service using the "myCache" key for a Redis client connection named "redis". The created IDistributedCache service can then be resolved using the "myCache" key: IServiceProvider serviceProvider = builder.Services.BuildServiceProvider(); var cache = serviceProvider.GetRequiredKeyedService<IDistributedCache>("myCache");

```csharp
var builder = WebApplication.CreateBuilder(args);

builder.AddRedisClientBuilder("redis")
       .WithKeyedDistributedCache("myCache", options => options.InstanceName = "myCache");
```
