diff --git a/RedisStudyTest/RedisHelperTest.cs b/RedisStudyTest/RedisHelperTest.cs index 3b58c70..a83f247 100644 --- a/RedisStudyTest/RedisHelperTest.cs +++ b/RedisStudyTest/RedisHelperTest.cs @@ -17,11 +17,6 @@ namespace RedisStudyTest { public class RedisHelperTest { - [Fact] - public void MyTest() - { - Assert.True(1 < 99); - } [Fact] public void GetRedisDatabaseTest() @@ -36,18 +31,9 @@ namespace RedisStudyTest [Fact] public void GetRedisDatabaseTest2() { - //ConfigurationOptions options = new ConfigurationOptions(); - //options.DefaultDatabase = 1; - //options.EndPoints.Add("127.0.0.1", 6379); - - //IConnectionMultiplexer connection = ConnectionMultiplexer.Connect(options); - - //var mock = new Mock(options); - ////mock.Setup(a => a.GetDatabase(1)).Returns(IDatabase); - - - //var myDB = mock.Object; - ////Assert.Equal("127.0.0.1", myDB.Multiplexer.Configuration); + var db = RedisHelper.GetConnectionMultiplexer(); + var db2 = RedisHelper.GetConnectionMultiplexer(); + Assert.Equal(db, db2); } [Fact] diff --git a/RedisStuy/RedisHelper.cs b/RedisStuy/RedisHelper.cs index 66a97e5..780c7dd 100644 --- a/RedisStuy/RedisHelper.cs +++ b/RedisStuy/RedisHelper.cs @@ -13,24 +13,37 @@ namespace RedisStuy /// /// 客户端操作 /// - public static class RedisHelper + public sealed class RedisHelper { - /// - /// 获取 Redis连接 - /// (此为共享和线程安全的,可以设计成单例模式) - /// - public static IConnectionMultiplexer GetConnectionMultiplexer() + + private static readonly IConnectionMultiplexer connectionMultiplexer = IniConnection(); + + private static IConnectionMultiplexer IniConnection() { ConfigurationOptions options = new ConfigurationOptions(); + options.KeepAlive = 200; options.DefaultDatabase = 1; options.AllowAdmin = true; options.EndPoints.Add("127.0.0.1", 6379); IConnectionMultiplexer connection = ConnectionMultiplexer.Connect(options); - return connection; } + private RedisHelper() + { + + } + + /// + /// 获取 Redis连接 + /// (此为共享和线程安全的,可以设计成单例模式) + /// + public static IConnectionMultiplexer GetConnectionMultiplexer() + { + return connectionMultiplexer; + } + /// /// 获取 默认Reids服务器