Skip to content
Docs Try Aspire

AspireRedisDistributedCacheExtensions Methods

Class Methods 4 members
Provides extension methods for adding Redis distributed caching services to an Hosting.IHostApplicationBuilder.
AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) Section titled AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) extension
Adds Redis distributed caching services, Distributed.IDistributedCache, in the services provided by the builder.
public static class AspireRedisDistributedCacheExtensions
{
public static void AddKeyedRedisDistributedCache(
this IHostApplicationBuilder builder,
string name,
Action<StackExchangeRedisSettings>? configureSettings = null,
Action<ConfigurationOptions>? configureOptions = null)
{
// ...
}
}
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.
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>) Section titled AddRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) extension
Adds Redis distributed caching services, Distributed.IDistributedCache, in the services provided by the builder.
public static class AspireRedisDistributedCacheExtensions
{
public static void AddRedisDistributedCache(
this IHostApplicationBuilder builder,
string connectionName,
Action<StackExchangeRedisSettings>? configureSettings = null,
Action<ConfigurationOptions>? configureOptions = null)
{
// ...
}
}
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.
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>) Section titled WithDistributedCache(AspireRedisClientBuilder, Action<RedisCacheOptions>) extension AspireRedisClientBuilder
Configures the Redis client to also provide distributed caching services through Distributed.IDistributedCache.
public static class AspireRedisDistributedCacheExtensions
{
public static AspireRedisClientBuilder WithDistributedCache(
this AspireRedisClientBuilder builder,
Action<RedisCacheOptions>? configureOptions = null)
{
// ...
}
}
builder AspireRedisClientBuilder The Redis.AspireRedisClientBuilder to configure.
configureOptions Action<RedisCacheOptions> optional An optional method that can be used for customizing the StackExchangeRedis.RedisCacheOptions.
AspireRedisClientBuilder The Redis.AspireRedisClientBuilder for method chaining.

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>();

var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClientBuilder("redis")
.WithDistributedCache();
WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>) Section titled WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>) extension AspireRedisClientBuilder
Configures the Redis client to provide a keyed distributed caching service through Distributed.IDistributedCache using name as the key.
public static class AspireRedisDistributedCacheExtensions
{
public static AspireRedisClientBuilder WithKeyedDistributedCache(
this AspireRedisClientBuilder builder,
string name,
Action<RedisCacheOptions>? configureOptions = null)
{
// ...
}
}
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.
AspireRedisClientBuilder The Redis.AspireRedisClientBuilder for method chaining.

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

var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClientBuilder("redis")
.WithKeyedDistributedCache("myCache", options => options.InstanceName = "myCache");