AspireRedisDistributedCacheExtensions Methods
Hosting.IHostApplicationBuilder. AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) Section titled AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) extension 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) { // ... }}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
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 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) { // ... }}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
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 Distributed.IDistributedCache. 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>();
var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClientBuilder("redis") .WithDistributedCache();WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>) Section titled WithKeyedDistributedCache(AspireRedisClientBuilder, string, Action<RedisCacheOptions>) extension AspireRedisClientBuilder Distributed.IDistributedCache using name as the key. 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");
var builder = WebApplication.CreateBuilder(args);
builder.AddRedisClientBuilder("redis") .WithKeyedDistributedCache("myCache", options => options.InstanceName = "myCache");