AspireRedisDistributedCacheExtensions.WithKeyedDistributedCache Method

Definition

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

public static Aspire.StackExchange.Redis.AspireRedisClientBuilder WithKeyedDistributedCache(this Aspire.StackExchange.Redis.AspireRedisClientBuilder builder, string name, Action<Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions>? configureOptions = default);
static member WithKeyedDistributedCache : Aspire.StackExchange.Redis.AspireRedisClientBuilder * string * Action<Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions> -> Aspire.StackExchange.Redis.AspireRedisClientBuilder
<Extension()>
Public Function WithKeyedDistributedCache (builder As AspireRedisClientBuilder, name As String, Optional configureOptions As Action(Of RedisCacheOptions) = Nothing) As AspireRedisClientBuilder

Parameters

name
String

The name which is used as the ServiceKey of the service.

configureOptions
Action<RedisCacheOptions>

An optional method that can be used for customizing the RedisCacheOptions.

Returns

The AspireRedisClientBuilder for method chaining.

Examples

The following example creates keyed IDistributedCache service using the "myCache" key for a Redis client connection named "redis".

var builder = WebApplication.CreateBuilder(args);

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

The created IDistributedCache service can then be resolved using the "myCache" key:

IServiceProvider serviceProvider = builder.Services.BuildServiceProvider();

var cache = serviceProvider.GetRequiredKeyedService<IDistributedCache>("myCache");

Applies to