Created
January 17, 2018 11:47
-
-
Save cristipufu/9ad47caf3dba60d712484d0c880597b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern alias redis; | |
using Castle.Core.Logging; | |
using redis::StackExchange.Redis; | |
using System; | |
using System.Net; | |
using UiPath.Core; | |
namespace UiPath.Web.Communication | |
{ | |
public static class RedisConnector | |
{ | |
public static ILogger Logger { get; set; } | |
public static IDatabase Database => ConnectionMultiplexer.GetDatabase(); | |
public static IServer Server => ConnectionMultiplexer.GetServer(GetEndpoint()); | |
public static ConnectionMultiplexer ConnectionMultiplexer => LazyCacheConnection.Value; | |
private static readonly Lazy<ConnectionMultiplexer> LazyCacheConnection = new Lazy<ConnectionMultiplexer>(() => | |
{ | |
var redisConnection = ConnectionMultiplexer.Connect(GetConfiguration()); | |
redisConnection.ConnectionFailed += (sender, args) => | |
{ | |
Logger.Error($"Redis Connection failed: {args.FailureType}, {args.Exception}"); | |
}; | |
redisConnection.ConnectionRestored += (sender, args) => | |
{ | |
Logger.Info("Redis Connection restored"); | |
}; | |
redisConnection.InternalError += (sender, args) => | |
{ | |
Logger.Error($"Redis Internal error: {args.Exception}, args.Origin"); | |
}; | |
redisConnection.ErrorMessage += (sender, args) => | |
{ | |
Logger.Error($"Redis error {args.Message}"); | |
}; | |
return redisConnection; | |
}); | |
private static ConfigurationOptions GetConfiguration() | |
{ | |
var config = new ConfigurationOptions | |
{ | |
AbortOnConnectFail = false, | |
Password = ConfigurationReader.RedisSettings.Password | |
}; | |
config.EndPoints.Add(GetEndpoint()); | |
return config; | |
} | |
private static EndPoint GetEndpoint() | |
{ | |
return new DnsEndPoint(ConfigurationReader.RedisSettings.Server, ConfigurationReader.RedisSettings.Port); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment