Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

c# - It was not possible to connect to the redis server(s); to create a disconnected multiplexer

I have the following piece of code to connect to azure redis cache.

   public class CacheConnectionHelper
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
        });

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }
    }

And I use it this way

public static List<Models.Module> GetModules()
        {
            IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
            List<Models.Module> listOfModules = new List<Models.Module>();
            listOfModules = (List<Models.Module>)cache.Get("ApplicationModules");
            if (listOfModules == null)
            {
                listOfModules = dbApp.Modulos.ToList();
                cache.Set("ApplicationModules", listOfModules, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
                return listOfModules;
            }
            else {
                return listOfModules;
            }
        }

However 1 or 2 times per day I get this exception:

Additional information: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING

The question is how can I refactor this code to go to the database in case the cache connection fails?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error you are getting is usually a sign that you have not set abortConnect=false in your connection string. The default value for abortConnect is true, which makes it so that StackExchange.Redis won't reconnect to the server automatically under some conditions. We strongly recommend that you set abortConnect=false in your connection string so that SE.Redis will auto-reconnect in the background if a network blip occurs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...