gpt4 book ai didi

c# - .net core IDistributedCache redis sentinel 支持master/slave

转载 作者:IT王子 更新时间:2023-10-29 06:06:10 35 4
gpt4 key购买 nike

我正在 .net core 2.0 中构建 web api 并将其部署在 kubernetes 上。我想将 IDistributedCache(带有 redis)与哨兵和主/从配置一起使用。我找不到任何相关文件。它如何处理主/从场景(故障转移情况)?

最佳答案

查看源代码应该会告诉您需要知道的一切: https://github.com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Redis/RedisCache.cs

如果您使用的是 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();
}

有一个活跃的请求让扩展将多路复用器公开为依赖注入(inject)服务: https://github.com/aspnet/Caching/issues/343

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

    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 包并且底层类更改了它的字段名称,那么您将想要抛出该异常。多路复用器应该在整个应用程序中共享,因此这是一个非常罕见的情况,您可能需要进行一些反射(reflection)。

编辑:我忘了提到如何做哨兵的事情: https://github.com/StackExchange/StackExchange.Redis/pull/406

edit2:反射的需求现在已经解决了,有了更新的版本

关于c# - .net core IDistributedCache redis sentinel 支持master/slave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403973/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com