问题
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?
回答1:
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.
回答2:
for beginners who dive in other's code an face this problem:
if (RedisConn == null)
{
ConfigurationOptions option = new ConfigurationOptions
{
AbortOnConnectFail = false,
EndPoints = { redisEndpoint }
};
RedisConn = ConnectionMultiplexer.Connect(option);
}
回答3:
You should also pay attention to the last part of your error message, as it seems to provide very useful details about the reason why connection have failed.
In your case:
It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING
My case:
It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. Timeout
回答4:
This problem was solved in a new release, the version 1.2.6 - you can see in Here
回答5:
For those maintaining older codebases, you may run into "It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING"
Once I upgraded to a more recent nuget package the error was still present but I got more error information: "The client and server cannot communicate, because they do not possess a common algorithm".
Once I applied the registry keys mentioned here I was ok. For those that don't wish to make that global change I believe there has been a PR for a setting.
来源:https://stackoverflow.com/questions/30895507/it-was-not-possible-to-connect-to-the-redis-servers-to-create-a-disconnected