问题
I am facing one problem that for instance my redis server was unavaialble at start but starts later. But due to above logic my Muxer object always remain un connected even though my redis is started. seems like there is no such thing as reconnect on failure or some thing.
I am using following for Redis Connection in my .net core 3.1 application
private static readonly Lazy Conn = new Lazy(
() =>
{
try
{ if (ConfigOptions != null && ConfigOptions.Value != null)
{
return ConnectionMultiplexer.Connect(ConfigOptions.Value);
}
return null;
}
catch (Exception ex)
{
Logger.Fatal(ex.Message, ex);
return null;
}
});
private static ConnectionMultiplexer Muxer => Conn.Value;
//http://gigi.nullneuron.net/gigilabs/setting-up-a-connection-with-stackexchange-redis/
//https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Configuration.md
private static readonly Lazy<ConfigurationOptions> ConfigOptions = new Lazy<ConfigurationOptions>(() =>
{
ConfigurationOptions configOptions = null;
configOptions = new ConfigurationOptions();
configOptions.EndPoints.Add(redisInfo.RedisServerNameOrIp + ":" + redisInfo.RedisServerPort);
configOptions.ClientName = "SafeRedisConnection_AuthApi_" + DateTime.Now.ToString();
if (redisInfo.ConnectTimeout != 0)
{
configOptions.ConnectTimeout = redisInfo.ConnectTimeout * 1000;
}
if (redisInfo.SyncTimeout != 0)
{
configOptions.SyncTimeout = redisInfo.SyncTimeout * 1000;
}
configOptions.AbortOnConnectFail = false;
if (redisInfo.KeepAliveDuration != 0)
{
configOptions.KeepAlive = redisInfo.KeepAliveDuration;
}
configOptions.DefaultDatabase = redisInfo.RedisDefaultDatabase;
if (!string.IsNullOrWhiteSpace(redisInfo.RedisServerPassword))
{
configOptions.Password = redisInfo.RedisServerPassword;
}
return configOptions;
});
My configOptions in debug mode is
来源:https://stackoverflow.com/questions/65261761/problem-in-lazy-connection-logic-in-stackexchenage-redis