AspireRedisDistributedCacheExtensions Methods
Hosting.IHostApplicationBuilder . AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>) Section titled AddKeyedRedisDistributedCache(IHostApplicationBuilder, string, Action<StackExchangeRedisSettings>, Action<ConfigurationOptions>)extensionDistributed.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
builderIHostApplicationBuilder The Hosting.IHostApplicationBuilder to read config from and add services to.namestringThe 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> optionalAn 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> optionalAn 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>)extensionDistributed.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
builderIHostApplicationBuilder The Hosting.IHostApplicationBuilder to read config from and add services to.connectionName stringA name used to retrieve the connection string from the ConnectionStrings configuration section.configureSettings Action<StackExchangeRedisSettings> optionalAn 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> optionalAn 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>)extensionAspireRedisClientBuilder Distributed.IDistributedCache . public static class AspireRedisDistributedCacheExtensions{ public static AspireRedisClientBuilder WithDistributedCache( this AspireRedisClientBuilder builder, Action<RedisCacheOptions>? configureOptions = null) { // ... }}Parameters
builderAspireRedisClientBuilder The Redis.AspireRedisClientBuilder to configure.configureOptions Action<RedisCacheOptions> optionalAn 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>)extensionAspireRedisClientBuilder 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
builderAspireRedisClientBuilder The Redis.AspireRedisClientBuilder to configure.namestringThe name which is used as the ServiceDescriptor.ServiceKey of the service.configureOptions Action<RedisCacheOptions> optionalAn 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");