Problem in Lazy Connection logic in stackexchenage.redis

杀马特。学长 韩版系。学妹 提交于 2021-01-07 01:36:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!