From 9df8d6d12aa362bbbdbc5f6d6bf707c28a52360d Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Fri, 13 Jul 2018 01:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=A6=81=E6=94=B9=E5=8F=98=EF=BC=9A?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E6=AC=A1=E8=BF=90=E8=A1=8C=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E6=B5=8B=E8=AF=95=E6=97=B6=EF=BC=8C=E5=8D=A1=E6=AD=BB?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=94=99=E8=AF=AF,=E5=B9=B6?= =?UTF-8?q?=E5=A4=A7=E5=A4=A7=E7=BC=A9=E7=9F=AD=E6=B5=8B=E8=AF=95=E6=97=B6?= =?UTF-8?q?=E9=97=B4=EF=BC=88=E5=88=86=E9=92=9F=E5=88=B0=E7=A7=92=EF=BC=89?= =?UTF-8?q?=20=E5=8E=9F=E5=9B=A0=E7=8C=9C=E6=B5=8B=EF=BC=9A=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=8E=B7=E5=8F=96=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=96=B0=E8=BF=9E=E6=8E=A5=EF=BC=8C=E5=AF=BC=E8=87=B4?= =?UTF-8?q?TCP/IP=E8=BF=9E=E6=8E=A5=E6=95=B0=E8=BF=87=E5=A4=A7=EF=BC=8C?= =?UTF-8?q?=E5=87=BA=E7=8E=B0Redis=E8=B6=85=E6=97=B6=E3=80=82=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=EF=BC=9A=E8=8E=B7=E5=8F=96IConnectionMultiplexer?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=8D=95=E4=BE=8B=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=A4=9A=E6=89=A3=E4=BD=8D=E5=85=B1=E4=BA=AB=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E8=BF=9E=E6=8E=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisHelperTest.cs | 20 +++----------------- RedisStuy/RedisHelper.cs | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 24 deletions(-) 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服务器