浏览代码

RedisHelper依赖注入实现

懒得勤快 7 年之前
父节点
当前提交
1cbb8fecd0

+ 3 - 3
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>netcoreapp2.0</TargetFramework>
-    <Version>2.1.1.1</Version>
+    <Version>2.1.2</Version>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。
@@ -85,10 +85,10 @@ string s = html.HtmlSantinizerStandard();//清理后:&lt;div&gt;&lt;span&gt;&l
 
   <ItemGroup>
     <PackageReference Include="AngleSharp" Version="0.9.10" />
-    <PackageReference Include="HtmlSanitizer" Version="4.0.187" />
+    <PackageReference Include="HtmlSanitizer" Version="4.0.197" />
     <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
     <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
-    <PackageReference Include="System.Drawing.Common" Version="4.5.0" />
+    <PackageReference Include="System.Drawing.Common" Version="4.5.1" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
Masuit.Tools.Core/NoSQL/RedisHelper.cs

@@ -19,7 +19,7 @@ namespace Masuit.Tools.NoSQL
         /// <summary>
         /// Redis服务器默认连接字符串,默认为:127.0.0.1:6379,allowadmin=true<br/>
         /// </summary>
-        public static string RedisConnectionString { get; set; } = "127.0.0.1:6379,allowadmin=true";
+        internal static string RedisConnectionString { get; set; } = "127.0.0.1:6379,allowadmin=true";
 
         /// <summary>
         /// 自定义键

+ 41 - 0
Masuit.Tools.Core/NoSQL/RedisHelperFactory.cs

@@ -0,0 +1,41 @@
+using Masuit.Tools.NoSQL;
+using System.Collections.Generic;
+
+namespace Masuit.Tools.Core.NoSQL
+{
+    public class RedisHelperFactory
+    {
+        internal static Dictionary<string, string> ConnectionCache { get; set; } = new Dictionary<string, string>();
+
+        /// <summary>
+        /// 创建一个Redis实例
+        /// </summary>
+        /// <param name="name"></param>
+        /// <param name="db"></param>
+        /// <returns></returns>
+        public RedisHelper Create(string name, int db = 0)
+        {
+            return RedisHelper.GetInstance(ConnectionCache[name], db);
+        }
+
+        /// <summary>
+        /// 创建一个默认实例
+        /// </summary>
+        /// <param name="db"></param>
+        /// <returns></returns>
+        public RedisHelper CreateDefault(int db = 0)
+        {
+            return RedisHelper.GetInstance(ConnectionCache["default"], db);
+        }
+
+        /// <summary>
+        /// 创建一个本地连接实例
+        /// </summary>
+        /// <param name="db"></param>
+        /// <returns></returns>
+        public RedisHelper CreateLocal(int db = 0)
+        {
+            return RedisHelper.GetInstance(ConnectionCache["local"], db);
+        }
+    }
+}

+ 24 - 0
Masuit.Tools.Core/NoSQL/ServiceCollectionExtensions.cs

@@ -0,0 +1,24 @@
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Masuit.Tools.Core.NoSQL
+{
+    public static class ServiceCollectionExtensions
+    {
+        public static IServiceCollection AddLocalRedisHelper(this IServiceCollection services)
+        {
+            return AddRedisHelper(services, "local");
+        }
+
+        public static IServiceCollection AddDefaultRedisHelper(this IServiceCollection services, string redisHost)
+        {
+            return AddRedisHelper(services, "default", redisHost);
+        }
+
+        public static IServiceCollection AddRedisHelper(this IServiceCollection services, string name, string redisHost = null)
+        {
+            RedisHelperFactory.ConnectionCache[name] = redisHost;
+            services.AddTransient<RedisHelperFactory>();
+            return services;
+        }
+    }
+}

+ 5 - 5
Masuit.Tools.sln

@@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.Core", "Masuit
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.NoSQL.MongoDBClient.Core", "Masuit.Tools.NoSQL.MongoDBClient.Core\Masuit.Tools.NoSQL.MongoDBClient.Core.csproj", "{85EEA76D-CDAB-44CB-A08C-A49FDBA4659D}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{0084ADDC-41F2-4452-BAF1-170B7F5213F2}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{875C5C44-2824-4A66-81E3-00190F2F7690}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -41,10 +41,10 @@ Global
 		{85EEA76D-CDAB-44CB-A08C-A49FDBA4659D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{85EEA76D-CDAB-44CB-A08C-A49FDBA4659D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{85EEA76D-CDAB-44CB-A08C-A49FDBA4659D}.Release|Any CPU.Build.0 = Release|Any CPU
-		{0084ADDC-41F2-4452-BAF1-170B7F5213F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{0084ADDC-41F2-4452-BAF1-170B7F5213F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{0084ADDC-41F2-4452-BAF1-170B7F5213F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{0084ADDC-41F2-4452-BAF1-170B7F5213F2}.Release|Any CPU.Build.0 = Release|Any CPU
+		{875C5C44-2824-4A66-81E3-00190F2F7690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{875C5C44-2824-4A66-81E3-00190F2F7690}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{875C5C44-2824-4A66-81E3-00190F2F7690}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{875C5C44-2824-4A66-81E3-00190F2F7690}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 21 - 0
NetCoreTest/Controllers/HomeController.cs

@@ -0,0 +1,21 @@
+using Masuit.Tools.Core.NoSQL;
+using Masuit.Tools.NoSQL;
+using Microsoft.AspNetCore.Mvc;
+
+namespace NetCoreTest.Controllers
+{
+    public class HomeController : Controller
+    {
+        public RedisHelper RedisHelper { get; set; }
+        public HomeController(RedisHelperFactory redisHelper)
+        {
+            RedisHelper = redisHelper.Create("aa");
+        }
+        // GET
+        public IActionResult Index()
+        {
+            RedisHelper.SetString("cc", 1);
+            return Content("111");
+        }
+    }
+}

+ 11 - 4
NetCoreTest/NetCoreTest.csproj

@@ -1,13 +1,20 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk.Web">
 
   <PropertyGroup>
-    <OutputType>Exe</OutputType>
-    <TargetFramework>netcoreapp2.0</TargetFramework>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
   </PropertyGroup>
 
+  <ItemGroup>
+    <Folder Include="wwwroot\" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.AspNetCore.App" />
+    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
+  </ItemGroup>
+
   <ItemGroup>
     <ProjectReference Include="..\Masuit.Tools.Core\Masuit.Tools.Core.csproj" />
-    <ProjectReference Include="..\Masuit.Tools.NoSQL.MongoDBClient.Core\Masuit.Tools.NoSQL.MongoDBClient.Core.csproj" />
   </ItemGroup>
 
 </Project>

+ 9 - 11
NetCoreTest/Program.cs

@@ -1,19 +1,17 @@
-using System;
-using Masuit.Tools;
-using Masuit.Tools.Core.Logging;
+using Microsoft.AspNetCore;
+using Microsoft.AspNetCore.Hosting;
 
 namespace NetCoreTest
 {
-    class Program
+    public class Program
     {
-        static void Main(string[] args)
+        public static void Main(string[] args)
         {
-            Console.WriteLine(AppContext.BaseDirectory);
-            LogManager.Event += info =>
-              {
-                  Console.WriteLine(info.ToJsonString());
-              };
-            LogManager.Error(new Exception("测试异常"));
+            CreateWebHostBuilder(args).Build().Run();
         }
+
+        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
+            WebHost.CreateDefaultBuilder(args)
+                .UseStartup<Startup>();
     }
 }

+ 29 - 0
NetCoreTest/Properties/launchSettings.json

@@ -0,0 +1,29 @@
+{
+  "iisSettings": {
+    "windowsAuthentication": false,
+    "anonymousAuthentication": true,
+    "iisExpress": {
+      "applicationUrl": "http://localhost:11070",
+      "sslPort": 0
+    }
+  },
+  "$schema": "http://json.schemastore.org/launchsettings.json",
+  "profiles": {
+    "IIS Express": {
+      "commandName": "IISExpress",
+      "launchBrowser": true,
+      "launchUrl": "api/values",
+      "environmentVariables": {
+        "ASPNETCORE_ENVIRONMENT": "Development"
+      }
+    },
+    "NetCoreTest": {
+      "commandName": "Project",
+      "launchUrl": "api/values",
+      "environmentVariables": {
+        "ASPNETCORE_ENVIRONMENT": "Development"
+      },
+      "applicationUrl": "http://localhost:5000"
+    }
+  }
+}

+ 39 - 0
NetCoreTest/Startup.cs

@@ -0,0 +1,39 @@
+using Masuit.Tools.Core.NoSQL;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace NetCoreTest
+{
+    public class Startup
+    {
+        public Startup(IConfiguration configuration)
+        {
+            Configuration = configuration;
+        }
+
+        public IConfiguration Configuration { get; }
+
+        // This method gets called by the runtime. Use this method to add services to the container.
+        public void ConfigureServices(IServiceCollection services)
+        {
+            services.AddDefaultRedisHelper("192.168.16.145:6379,password=xilife2018,connectTimeout=1000,connectRetry=1,syncTimeout=1000");
+            services.AddLocalRedisHelper();
+            services.AddRedisHelper("aa", "192.168.16.145:6379,password=xilife2018,connectTimeout=1000,connectRetry=1,syncTimeout=1000");
+            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
+        }
+
+        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
+        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
+        {
+            if (env.IsDevelopment())
+            {
+                app.UseDeveloperExceptionPage();
+            }
+
+            app.UseMvcWithDefaultRoute();
+        }
+    }
+}

+ 9 - 0
NetCoreTest/appsettings.Development.json

@@ -0,0 +1,9 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Debug",
+      "System": "Information",
+      "Microsoft": "Information"
+    }
+  }
+}

+ 8 - 0
NetCoreTest/appsettings.json

@@ -0,0 +1,8 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Warning"
+    }
+  },
+  "AllowedHosts": "*"
+}