diff --git a/HybirdFrameworkCore/HybirdFrameworkCore.csproj b/HybirdFrameworkCore/HybirdFrameworkCore.csproj
index 4cc6f7a..8526592 100644
--- a/HybirdFrameworkCore/HybirdFrameworkCore.csproj
+++ b/HybirdFrameworkCore/HybirdFrameworkCore.csproj
@@ -12,6 +12,7 @@
+
diff --git a/HybirdFrameworkCore/Redis/RedisHelper.cs b/HybirdFrameworkCore/Redis/RedisHelper.cs
new file mode 100644
index 0000000..085e220
--- /dev/null
+++ b/HybirdFrameworkCore/Redis/RedisHelper.cs
@@ -0,0 +1,137 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using HybirdFrameworkCore.Redis;
+using Newtonsoft.Json;
+using StackExchange.Redis;
+
+namespace HybirdFrameworkCore.Redis
+
+{
+ public class RedisHelper : IDisposable
+ {
+ //连接字符串
+ private string _connectionString;
+ //实例名称
+ private string _instanceName;
+ //默认数据库
+ private int _defaultDB;
+
+ private ConcurrentDictionary _connections;
+ ///
+ ///
+ ///
+ /// 连接字符串
+ /// 实例名称
+ /// 默认数据库
+ public RedisHelper(string connectionString, string instanceName, int defaultDB = 0)
+ {
+ _connectionString = connectionString;
+ _instanceName = instanceName;
+ _defaultDB = defaultDB;
+ _connections = new ConcurrentDictionary();
+ }
+
+ ///
+ /// 获取ConnectionMultiplexer
+ ///
+ ///
+ private ConnectionMultiplexer GetConnect()
+ {
+ return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString));
+ }
+
+ ///
+ /// 获取数据库
+ ///
+ ///
+ /// 默认为0:优先代码的db配置,其次config中的配置
+ ///
+ public IDatabase GetDatabase()
+ {
+ return GetConnect().GetDatabase(_defaultDB);
+ }
+
+ ///
+ /// 获取数据库
+ ///
+ ///
+ /// 默认为0:优先代码的db配置,其次config中的配置
+ ///
+ public IDatabase GetDatabase(int defaultDB)
+ {
+ return GetConnect().GetDatabase(_defaultDB);
+ }
+
+ public IServer GetServer(string configName = null, int endPointsIndex = 0)
+ {
+ var confOption = ConfigurationOptions.Parse(_connectionString);
+ return GetConnect().GetServer(confOption.EndPoints[endPointsIndex]);
+ }
+
+ public ISubscriber GetSubscriber(string configName = null)
+ {
+ return GetConnect().GetSubscriber();
+ }
+ public void Dispose()
+ {
+ if (_connections != null && _connections.Count > 0)
+ {
+ foreach (var item in _connections.Values)
+ {
+ item.Close();
+ }
+ }
+ }
+
+ ///
+ /// 获取Redis中的值(同步版本)
+ ///
+ /// 键
+ /// 对应的值,如果键不存在则返回null
+ public string GetStrValue(string key)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ return db.StringGet(key);
+ }
+
+ ///
+ /// 获取Redis中的值(异步版本)
+ ///
+ /// 键
+ /// 一个表示异步操作的任务,任务的结果是对应的值,如果键不存在则返回null
+ public async Task GetAsync(string key)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ return await db.StringGetAsync(key);
+ }
+
+ ///
+ /// 设置Redis中的值(同步版本)
+ ///
+ /// 键
+ /// 值
+ /// 操作是否成功
+ public bool SetKeyValueStr(string key, string value)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ return db.StringSet(key, value);
+ }
+
+ ///
+ /// 设置Redis中的值(异步版本)
+ ///
+ /// 键
+ /// 值
+ /// 一个表示异步操作的任务
+ public async Task SetAsync(string key, string value)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ return await db.StringSetAsync(key, value);
+ }
+ }
+
+
+}
diff --git a/HybirdFrameworkCore/bin/Debug/net6.0/HybirdFrameworkCore.deps.json b/HybirdFrameworkCore/bin/Debug/net6.0/HybirdFrameworkCore.deps.json
index 42fc25d..0965e5d 100644
--- a/HybirdFrameworkCore/bin/Debug/net6.0/HybirdFrameworkCore.deps.json
+++ b/HybirdFrameworkCore/bin/Debug/net6.0/HybirdFrameworkCore.deps.json
@@ -12,6 +12,7 @@
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
"Microsoft.Extensions.Configuration.Json": "7.0.0",
"Newtonsoft.Json": "13.0.3",
+ "StackExchange.Redis": "2.7.33",
"log4net": "2.0.15"
},
"runtime": {
@@ -125,6 +126,14 @@
}
}
},
+ "Microsoft.Extensions.Logging.Abstractions/6.0.0": {
+ "runtime": {
+ "lib/net6.0/Microsoft.Extensions.Logging.Abstractions.dll": {
+ "assemblyVersion": "6.0.0.0",
+ "fileVersion": "6.0.21.52210"
+ }
+ }
+ },
"Microsoft.Extensions.Primitives/7.0.0": {
"dependencies": {
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
@@ -145,6 +154,29 @@
}
}
},
+ "Pipelines.Sockets.Unofficial/2.2.8": {
+ "dependencies": {
+ "System.IO.Pipelines": "5.0.1"
+ },
+ "runtime": {
+ "lib/net5.0/Pipelines.Sockets.Unofficial.dll": {
+ "assemblyVersion": "1.0.0.0",
+ "fileVersion": "2.2.8.1080"
+ }
+ }
+ },
+ "StackExchange.Redis/2.7.33": {
+ "dependencies": {
+ "Microsoft.Extensions.Logging.Abstractions": "6.0.0",
+ "Pipelines.Sockets.Unofficial": "2.2.8"
+ },
+ "runtime": {
+ "lib/net6.0/StackExchange.Redis.dll": {
+ "assemblyVersion": "2.0.0.0",
+ "fileVersion": "2.7.33.41805"
+ }
+ }
+ },
"System.Configuration.ConfigurationManager/4.5.0": {
"dependencies": {
"System.Security.Cryptography.ProtectedData": "4.5.0",
@@ -158,6 +190,14 @@
}
},
"System.Diagnostics.DiagnosticSource/4.7.1": {},
+ "System.IO.Pipelines/5.0.1": {
+ "runtime": {
+ "lib/netcoreapp3.0/System.IO.Pipelines.dll": {
+ "assemblyVersion": "5.0.0.1",
+ "fileVersion": "5.0.120.57516"
+ }
+ }
+ },
"System.Runtime.CompilerServices.Unsafe/6.0.0": {},
"System.Security.AccessControl/4.5.0": {
"dependencies": {
@@ -299,6 +339,13 @@
"path": "microsoft.extensions.filesystemglobbing/7.0.0",
"hashPath": "microsoft.extensions.filesystemglobbing.7.0.0.nupkg.sha512"
},
+ "Microsoft.Extensions.Logging.Abstractions/6.0.0": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-/HggWBbTwy8TgebGSX5DBZ24ndhzi93sHUBDvP1IxbZD7FDokYzdAr6+vbWGjw2XAfR2EJ1sfKUotpjHnFWPxA==",
+ "path": "microsoft.extensions.logging.abstractions/6.0.0",
+ "hashPath": "microsoft.extensions.logging.abstractions.6.0.0.nupkg.sha512"
+ },
"Microsoft.Extensions.Primitives/7.0.0": {
"type": "package",
"serviceable": true,
@@ -320,6 +367,20 @@
"path": "newtonsoft.json/13.0.3",
"hashPath": "newtonsoft.json.13.0.3.nupkg.sha512"
},
+ "Pipelines.Sockets.Unofficial/2.2.8": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-zG2FApP5zxSx6OcdJQLbZDk2AVlN2BNQD6MorwIfV6gVj0RRxWPEp2LXAxqDGZqeNV1Zp0BNPcNaey/GXmTdvQ==",
+ "path": "pipelines.sockets.unofficial/2.2.8",
+ "hashPath": "pipelines.sockets.unofficial.2.2.8.nupkg.sha512"
+ },
+ "StackExchange.Redis/2.7.33": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-2kCX5fvhEE824a4Ab5Imyi8DRuGuTxyklXV01kegkRpsWJcPmO6+GAQ+HegKxvXAxlXZ8yaRspvWJ8t3mMClfQ==",
+ "path": "stackexchange.redis/2.7.33",
+ "hashPath": "stackexchange.redis.2.7.33.nupkg.sha512"
+ },
"System.Configuration.ConfigurationManager/4.5.0": {
"type": "package",
"serviceable": true,
@@ -334,6 +395,13 @@
"path": "system.diagnostics.diagnosticsource/4.7.1",
"hashPath": "system.diagnostics.diagnosticsource.4.7.1.nupkg.sha512"
},
+ "System.IO.Pipelines/5.0.1": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-qEePWsaq9LoEEIqhbGe6D5J8c9IqQOUuTzzV6wn1POlfdLkJliZY3OlB0j0f17uMWlqZYjH7txj+2YbyrIA8Yg==",
+ "path": "system.io.pipelines/5.0.1",
+ "hashPath": "system.io.pipelines.5.0.1.nupkg.sha512"
+ },
"System.Runtime.CompilerServices.Unsafe/6.0.0": {
"type": "package",
"serviceable": true,
diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs
index 8914a1d..5fe1276 100644
--- a/WebStarter/Program.cs
+++ b/WebStarter/Program.cs
@@ -2,6 +2,7 @@ using Autofac;
using Autofac.Extensions.DependencyInjection;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
+using HybirdFrameworkCore.Redis;
using HybirdFrameworkDriver.TcpServer;
using SqlSugar;
using SqlSugar.IOC;
@@ -16,10 +17,10 @@ builder.Host.ConfigureContainer(cb =>
var db = new SqlSugarClient(new ConnectionConfig
{
ConfigId = AppSettingsConstVars.ConfigId,
- ConnectionString = AppSettingsConstVars.DbSqlConnection, // ݿַ
+ ConnectionString = AppSettingsConstVars.DbSqlConnection, // �������ݿ������ַ���
DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer,
IsAutoCloseConnection = true,
- InitKeyType = InitKeyType.Attribute // ʹʵԽʶΪ InitKeyType.Attribute
+ InitKeyType = InitKeyType.Attribute // ���ʹ��ʵ��������Խ���������ʶ��������Ϊ InitKeyType.Attribute
});
return db;
}).As().InstancePerLifetimeScope();
@@ -29,6 +30,18 @@ builder.Host.ConfigureContainer(cb =>
cb.RegisterModule(new AutofacModuleRegister());
});
+//redis
+var section = builder.Configuration.GetSection("Redis");
+//连接字符串
+string? redisConnectionString = section.GetSection("Connection").Value;
+//实例名称
+string? instanceName = section.GetSection("InstanceName").Value;
+//默认数据库
+int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
+if (redisConnectionString != null && instanceName != null)
+ builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, _defaultDB));
+
+
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
diff --git a/WebStarter/appsettings.json b/WebStarter/appsettings.json
index 4db5f7b..341b3dc 100644
--- a/WebStarter/appsettings.json
+++ b/WebStarter/appsettings.json
@@ -9,6 +9,11 @@
"Version": "1.1.0.1",
"Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml"
},
+ "Redis": {
+ "Connection": "106.12.36.89:6379,password=123456",
+ "InstanceName": "local",
+ "DefaultDB": "8"
+ },
"Logging": {
"LogLevel": {
"Default": "Information",