StackExchange.Redis的Basic Usage文档解释说ConnectionMultiplexer
是长期存在的,预计会被重用。
但是当与服务器的连接断开时呢? ConnectionMultiplexer
会自动重新连接,还是必须像this answer中那样编写代码(引用该答案):
if (RedisConnection == null || !RedisConnection.IsConnected)
{
RedisConnection = ConnectionMultiplexer.Connect(...);
}
RedisCacheDb = RedisConnection.GetDatabase();
上述代码是否适合处理断开连接的恢复,或者它实际上会导致多个ConnectionMultiplexer
实例?同样,如何解释IsConnected
属性?
[旁白:我相信上面的代码是一种非常糟糕的延迟初始化形式,特别是在多线程环境中 - 请参阅Jon Skeet's article on Singletons]。
答案 0 :(得分:25)
以下是pattern recommended by the Azure Redis Cache team:
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => {
return ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});
public static ConnectionMultiplexer Connection {
get {
return lazyConnection.Value;
}
}
一些要点:
答案 1 :(得分:0)
是的,您需要这种类型的验证才能修复断开的连接。还应该考虑一些线程安全性。这就是我通常这样做的方式:
private static ConnectionMultiplexer _redis;
private static readonly Object _multiplexerLock = new Object();
private void ConnectRedis()
{
try
{
_redis = ConnectionMultiplexer.Connect("...<connection string here>...");
}
catch (Exception ex)
{
//exception handling goes here
}
}
private ConnectionMultiplexer RedisMultiplexer
{
get
{
lock (_multiplexerLock)
{
if (_redis == null || !_redis.IsConnected)
{
ConnectRedis();
}
return _redis;
}
}
}
然后我在需要调用Redis端点的地方使用RedisMultiplexer
属性。我通常不会存储GetDatabase()
电话的结果,因为文档说这是一个非常轻量级的电话。