Przeglądaj źródła

😁升级sqlsugar,优化仓储并发速度

zuohuaijun 2 lat temu
rodzic
commit
6ecb9eeb8a

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -39,7 +39,7 @@
     <PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.1" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="3.0.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="3.1.0" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.4.145" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.4.146-preview08" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.10" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 9 - 4
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -10,28 +10,33 @@ namespace Admin.NET.Core;
 /// <typeparam name="T"></typeparam>
 public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
 {
+    protected ITenant iTenant = null;
+
     public SqlSugarRepository()
     {
-        base.Context = SqlSugarSetup.GetConnectionScope(typeof(SysTableAttribute));
+        iTenant = App.GetRequiredService<ISqlSugarClient>().AsTenant();
+        base.Context = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
 
         // 若实体贴有多库特性,则返回指定库连接
         if (typeof(T).IsDefined(typeof(TenantAttribute), false))
         {
-            base.Context = SqlSugarSetup.GetConnectionScope(typeof(T));
+            base.Context = iTenant.GetConnectionScopeWithAttr<T>();
             return;
         }
 
         // 若实体贴有日志表特性,则返回日志库连接
         if (typeof(T).IsDefined(typeof(LogTableAttribute), false))
         {
-            base.Context = SqlSugarSetup.GetConnectionScope(typeof(LogTableAttribute));
+            base.Context = iTenant.IsAnyConnection(SqlSugarConst.LogConfigId)
+                ? iTenant.GetConnectionScope(SqlSugarConst.LogConfigId)
+                : iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
             return;
         }
 
         // 若实体贴有系统表特性,则返回默认库连接
         if (typeof(T).IsDefined(typeof(SysTableAttribute), false))
         {
-            base.Context = SqlSugarSetup.GetConnectionScope(typeof(SysTableAttribute));
+            base.Context = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
             return;
         }
 

+ 0 - 45
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -6,9 +6,6 @@ namespace Admin.NET.Core;
 
 public static class SqlSugarSetup
 {
-    // 缓存所有仓储连接实例
-    private static readonly ConcurrentDictionary<Type, ISqlSugarClient> sqlSugarClientDict = new();
-
     /// <summary>
     /// SqlSugar 上下文初始化
     /// </summary>
@@ -42,7 +39,6 @@ public static class SqlSugarSetup
                 SetDbDiffLog(dbProvider, config);
             });
         });
-        InitSqlSugarRepository(sqlSugar.AsTenant()); // 初始化仓储实例
 
         services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
         services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
@@ -373,45 +369,4 @@ public static class SqlSugarSetup
                 db.CodeFirst.SplitTables().InitTables(entityType);
         }
     }
-
-    /// <summary>
-    /// 初始化仓储连接实例
-    /// </summary>
-    /// <param name="iTenant"></param>
-    public static void InitSqlSugarRepository(ITenant iTenant)
-    {
-        // 主库仓储实例
-        var iClientMain = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
-        sqlSugarClientDict.TryAdd(typeof(SysTableAttribute), iClientMain);
-
-        // 日志库仓储实例
-        var iClientLog = iTenant.IsAnyConnection(SqlSugarConst.LogConfigId)
-                ? iTenant.GetConnectionScope(SqlSugarConst.LogConfigId)
-                : iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
-        sqlSugarClientDict.TryAdd(typeof(LogTableAttribute), iClientLog);
-
-        // 其他库仓储实例
-        var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
-            && u.IsDefined(typeof(SugarTable), false) && u.GetCustomAttribute<TenantAttribute>() != null).ToList();
-        foreach (var entityType in entityTypes)
-        {
-            MethodInfo genericMethod = typeof(ITenant).GetMethod("GetConnectionScopeWithAttr");
-            MethodInfo constructedMethod = genericMethod.MakeGenericMethod(entityType);
-            ISqlSugarClient iClientAttr = constructedMethod.Invoke(iTenant, null) as ISqlSugarClient;
-            sqlSugarClientDict.TryAdd(entityType, iClientAttr);
-        }
-    }
-
-    /// <summary>
-    /// 获取指定仓储连接实例
-    /// </summary>
-    /// <param name="type"></param>
-    /// <returns></returns>
-    public static ISqlSugarClient GetConnectionScope(Type type)
-    {
-        sqlSugarClientDict.TryGetValue(type, out ISqlSugarClient sqlSugarClient);
-        _ = sqlSugarClient.Aop;
-        _ = sqlSugarClient.QueryFilter;
-        return sqlSugarClient;
-    }
 }