How do I use multiple instances of redis in net core along with the DI container?
On startup I have the following:
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = distributedCacheConnectionString;
});
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = myAppSessionCacheConnectionString;
});
For injection I would have something like the following in a constructor for example:
namespace MyApp
{
public class MyClass
{
public MyClass(IDistributedCache myAppSessionCache)
{
// use a specific redis cache instance here
_distributedCache = myAppSessionCache;
}
}
}
namespace MyApp
{
public class MyClass2
{
public MyClass2(IDistributedCache distributedCache)
{
// use a specific redis cache instance here
_distributedCache = distributedCache;
}
}
}
I know for net core the DI container is limited compared something like Unity or Spring etc... I know for multiple implementations of an interface there are several tricks, one is injecting an IEnumerable of the interface, another example is typed interfaces, etc... I just don't see the option with the redis cache and we do have a need for an application to communicate with 2 separate redis servers within the system.
I am mostly looking for the path of least resistance. I can always create a wrapper around the Redis Client. I was just hoping that something existed but I was just overlooking it.