集成redis

master
lxw 7 months ago
parent 500d02286c
commit 2c7cf28be6

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -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<string, ConnectionMultiplexer> _connections;
/// <summary>
///
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="instanceName">实例名称</param>
/// <param name="defaultDB">默认数据库</param>
public RedisHelper(string connectionString, string instanceName, int defaultDB = 0)
{
_connectionString = connectionString;
_instanceName = instanceName;
_defaultDB = defaultDB;
_connections = new ConcurrentDictionary<string, ConnectionMultiplexer>();
}
/// <summary>
/// 获取ConnectionMultiplexer
/// </summary>
/// <returns></returns>
private ConnectionMultiplexer GetConnect()
{
return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString));
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="configName"></param>
/// <param name="db">默认为0优先代码的db配置其次config中的配置</param>
/// <returns></returns>
public IDatabase GetDatabase()
{
return GetConnect().GetDatabase(_defaultDB);
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="configName"></param>
/// <param name="db">默认为0优先代码的db配置其次config中的配置</param>
/// <returns></returns>
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();
}
}
}
/// <summary>
/// 获取Redis中的值同步版本
/// </summary>
/// <param name="key">键</param>
/// <returns>对应的值如果键不存在则返回null</returns>
public string GetStrValue(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.StringGet(key);
}
/// <summary>
/// 获取Redis中的值异步版本
/// </summary>
/// <param name="key">键</param>
/// <returns>一个表示异步操作的任务任务的结果是对应的值如果键不存在则返回null</returns>
public async Task<string> GetAsync(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringGetAsync(key);
}
/// <summary>
/// 设置Redis中的值同步版本
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>操作是否成功</returns>
public bool SetKeyValueStr(string key, string value)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.StringSet(key, value);
}
/// <summary>
/// 设置Redis中的值异步版本
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>一个表示异步操作的任务</returns>
public async Task<bool> SetAsync(string key, string value)
{
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringSetAsync(key, value);
}
}
}

@ -12,6 +12,7 @@
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0", "Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
"Microsoft.Extensions.Configuration.Json": "7.0.0", "Microsoft.Extensions.Configuration.Json": "7.0.0",
"Newtonsoft.Json": "13.0.3", "Newtonsoft.Json": "13.0.3",
"StackExchange.Redis": "2.7.33",
"log4net": "2.0.15" "log4net": "2.0.15"
}, },
"runtime": { "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": { "Microsoft.Extensions.Primitives/7.0.0": {
"dependencies": { "dependencies": {
"System.Runtime.CompilerServices.Unsafe": "6.0.0" "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": { "System.Configuration.ConfigurationManager/4.5.0": {
"dependencies": { "dependencies": {
"System.Security.Cryptography.ProtectedData": "4.5.0", "System.Security.Cryptography.ProtectedData": "4.5.0",
@ -158,6 +190,14 @@
} }
}, },
"System.Diagnostics.DiagnosticSource/4.7.1": {}, "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.Runtime.CompilerServices.Unsafe/6.0.0": {},
"System.Security.AccessControl/4.5.0": { "System.Security.AccessControl/4.5.0": {
"dependencies": { "dependencies": {
@ -299,6 +339,13 @@
"path": "microsoft.extensions.filesystemglobbing/7.0.0", "path": "microsoft.extensions.filesystemglobbing/7.0.0",
"hashPath": "microsoft.extensions.filesystemglobbing.7.0.0.nupkg.sha512" "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": { "Microsoft.Extensions.Primitives/7.0.0": {
"type": "package", "type": "package",
"serviceable": true, "serviceable": true,
@ -320,6 +367,20 @@
"path": "newtonsoft.json/13.0.3", "path": "newtonsoft.json/13.0.3",
"hashPath": "newtonsoft.json.13.0.3.nupkg.sha512" "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": { "System.Configuration.ConfigurationManager/4.5.0": {
"type": "package", "type": "package",
"serviceable": true, "serviceable": true,
@ -334,6 +395,13 @@
"path": "system.diagnostics.diagnosticsource/4.7.1", "path": "system.diagnostics.diagnosticsource/4.7.1",
"hashPath": "system.diagnostics.diagnosticsource.4.7.1.nupkg.sha512" "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": { "System.Runtime.CompilerServices.Unsafe/6.0.0": {
"type": "package", "type": "package",
"serviceable": true, "serviceable": true,

@ -2,6 +2,7 @@ using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Redis;
using HybirdFrameworkDriver.TcpServer; using HybirdFrameworkDriver.TcpServer;
using SqlSugar; using SqlSugar;
using SqlSugar.IOC; using SqlSugar.IOC;
@ -16,10 +17,10 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
var db = new SqlSugarClient(new ConnectionConfig var db = new SqlSugarClient(new ConnectionConfig
{ {
ConfigId = AppSettingsConstVars.ConfigId, ConfigId = AppSettingsConstVars.ConfigId,
ConnectionString = AppSettingsConstVars.DbSqlConnection, // 设置数据库连接字符串 ConnectionString = AppSettingsConstVars.DbSqlConnection, // <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer, DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer,
IsAutoCloseConnection = true, IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute // 如果使用实体类的属性进行主键标识,请设置为 InitKeyType.Attribute InitKeyType = InitKeyType.Attribute // <EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ InitKeyType.Attribute
}); });
return db; return db;
}).As<ISqlSugarClient>().InstancePerLifetimeScope(); }).As<ISqlSugarClient>().InstancePerLifetimeScope();
@ -29,6 +30,18 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
cb.RegisterModule(new AutofacModuleRegister()); 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(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

@ -9,6 +9,11 @@
"Version": "1.1.0.1", "Version": "1.1.0.1",
"Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml" "Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml"
}, },
"Redis": {
"Connection": "106.12.36.89:6379,password=123456",
"InstanceName": "local",
"DefaultDB": "8"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

Loading…
Cancel
Save