本文介绍了这是不可能连接到redis的服务器(S);以创建断开连接多路复用器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段代码连接到Azure的Redis的缓存。

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;
            }
        }
    }

和我使用这种方式

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;
            }
        }



不过,每天1〜2次,我得到这个例外

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?

推荐答案

你所得到的错误通常是你没有设置标志abortConnect = FALSE 在你的连接字符串。为的 abortConnect 的默认值是的真正的,这使得它,这样的 StackExchange.Redis 的不会在某些情况下重新连接到服务器自动。我们强烈建议您设置的 abortConnect = FALSE 的连接字符串中,这样的 SE.Redis 的将在后台自动重新连接如果发生网络昙花一现。

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.

这篇关于这是不可能连接到redis的服务器(S);以创建断开连接多路复用器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:26