C# .net核心IDistributedCache redis sentinel对主/从设备的支持

C# .net核心IDistributedCache redis sentinel对主/从设备的支持,c#,redis,.net-core,C#,Redis,.net Core,我正在.net core 2.0中构建web API,并将其部署到kubernetes上。我希望使用IDistributedCache(带redis)和sentinel以及主/从配置。我找不到这方面的任何文件。它如何处理主/从场景(故障转移情况)?查看源代码应该可以告诉您需要知道的一切: 如果您使用的是nuget扩展,那么IDistributedCache实例应该是RedisCache类型。你应该能够投下它: public RedisWrapperClassConstructor(ID

我正在.net core 2.0中构建web API,并将其部署到kubernetes上。我希望使用IDistributedCache(带redis)和sentinel以及主/从配置。我找不到这方面的任何文件。它如何处理主/从场景(故障转移情况)?

查看源代码应该可以告诉您需要知道的一切:

如果您使用的是nuget扩展,那么IDistributedCache实例应该是RedisCache类型。你应该能够投下它:

    public RedisWrapperClassConstructor(IDistributedCache c)
    {
        cche = c as RedisCache;
        //since pub/sub is only supported by Redis, we need to force the issue
        if (cche == null) throw new InvalidCastException("distributed cache must be of type RedisCache");
        EnsureTheConnectionMultiplexerIsSet();
    }
有一个活动请求,要求扩展将多路复用器作为依赖项注入服务公开:

现在,您必须使用丑陋的反射:

    private void EnsureTheConnectionMultiplexerIsSet()
    {
        if (_connection != null) return;
        cche.Get("startup"); //populate the connection with a dummy request
        var _connectionMultiplexorFieldInfo = typeof(RedisCache).GetField(nameof(_connection), BindingFlags.NonPublic | BindingFlags.Instance);
        var vl = _connectionMultiplexorFieldInfo.GetValue(cche);
        if (vl != null)
        {
            _connection = (ConnectionMultiplexer)vl;
            if (_connection == null) throw new InvalidCastException("Could not cast the ConnectionMultiplexer");
        }
        if (_connection == null) throw new InvalidCastException("Could not access the ConnectionMultiplexer");
    }
最重要的部分是例外情况。如果您更新了nuget包,并且基础类更改了它的字段名,那么您将希望抛出该异常。多路复用器应该在整个应用程序中共享,因此这是一个非常罕见的实例,您可能需要进行一些反射

编辑:我忘了提到如何做哨兵的事情:

我很惊讶两年后仍然没有哨兵支持