Open
Description
The current implementation doesn't support multiple IDistributedCache
instances to be registered, either using dedicated Redis instances or a shared one with prefixes.
This is due to how the IDistributedCache
is used in aspnetcore and also how the extension method to register the service is designed. This is confusing users as reported here and from internal usage.
The suggestion is to change the current API to support these scenarios.
The following usage would then be possible:
builder.AddRedisDistributedCache("redis1");
builder.AddKeyedRedisDistributedCache(name: "redis2");
builder.AddKeyedRedisDistributedCache(name: "redis2", serviceKey: "redis3");
This would result in:
- a non-keyed
IDistributedCache
service registration using the Redis resourceredis1
- a keyed
IDistributedCache
service using a different Redis resourceredis2
, and no key prefix - a keyed
IDistributedCache
service using the same Redis resourceredis2
, but with the"redis3"
service key, and the prefix"redis3"
for its keys
Service resolution would be done like so:
app.MapGet("/cache1", async (IDistributedCache cache) => { ... });
app.MapGet("/cache2", async ([FromKeyedServices("redis2")] IDistributedCache cache) => { ... });
app.MapGet("/cache3", async ([FromKeyedServices("redis3")] IDistributedCache cache) => { ... });