修复清理vengine
This commit is contained in:
@@ -1,30 +1,32 @@
|
||||
using JNPF;
|
||||
using System.Collections.Concurrent;
|
||||
using JNPF;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SqlSugar;
|
||||
using System.Collections.Concurrent;
|
||||
using Tnb.Core;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine.DataAccess;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
///
|
||||
/// </summary>
|
||||
public class DataAccess : IDataAccess, ITransient, IDisposable
|
||||
{
|
||||
const int MAX_PAGE_SIZE = 1000;
|
||||
private const int MAX_PAGE_SIZE = 1000;
|
||||
private ISqlSugarClient? _db;
|
||||
|
||||
protected ISqlSugarClient Db
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_db == null)
|
||||
{
|
||||
var dbType = App.Configuration.GetConnectionString("DbType");
|
||||
var dbConn = App.Configuration.GetConnectionString("DbConn");
|
||||
_db = SugarHelper.CreateSugarClient("_db_default_", dbType, dbConn);
|
||||
ConnectionStringsOptions conn = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
//var dbType = App.Configuration.GetConnectionString("DbType");
|
||||
//var dbConn = App.Configuration.GetConnectionString("DbConn");
|
||||
_db = SugarHelper.CreateSugarClient(conn.ConfigId, conn.DBType, conn.ConnectString);
|
||||
}
|
||||
return _db;
|
||||
}
|
||||
@@ -35,7 +37,7 @@ public class DataAccess : IDataAccess, ITransient, IDisposable
|
||||
/// <summary>
|
||||
/// 全局缓存
|
||||
/// </summary>
|
||||
static ConcurrentDictionary<string, ISqlSugarClient> DbCache = new ConcurrentDictionary<string, ISqlSugarClient>();
|
||||
private static ConcurrentDictionary<string, ISqlSugarClient> DbCache = new ConcurrentDictionary<string, ISqlSugarClient>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造
|
||||
@@ -290,9 +292,7 @@ public class DataAccess : IDataAccess, ITransient, IDisposable
|
||||
{
|
||||
ret.Add(prop.navCode, new List<DObject>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -424,5 +424,4 @@ public class DataAccess : IDataAccess, ITransient, IDisposable
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Tnb.Vengine.Domain;
|
||||
namespace Tnb.Vengine.DataAccess;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
///
|
||||
/// </summary>
|
||||
public interface IDataAccess : ITransient
|
||||
{
|
||||
@@ -27,18 +27,22 @@ public interface IDataAccess : ITransient
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
|
||||
//Task<VmPagedOutput> QueryDataAsync(VmBaseInput input);
|
||||
|
||||
/// <summary>
|
||||
@@ -63,4 +67,4 @@ public interface IDataAccess : ITransient
|
||||
/// 删除数据 默认方法
|
||||
/// </summary>
|
||||
Task<int> DeleteDataAsync(Vmodel vm, VmDeleteInput input);
|
||||
}
|
||||
}
|
||||
@@ -7,67 +7,67 @@ namespace Tnb.Vengine.DataAccess;
|
||||
|
||||
public class SugarHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建SugarClient实例
|
||||
/// </summary>
|
||||
public static ISqlSugarClient CreateSugarClient(string dbCode, string dbType, string dbConnection, Action<ConnectionConfig>? configConnection = null, Action<ISqlSugarClient>? configSugar = null)
|
||||
{
|
||||
var config = new ConnectionConfig
|
||||
/// <summary>
|
||||
/// 创建SugarClient实例
|
||||
/// </summary>
|
||||
public static ISqlSugarClient CreateSugarClient(string dbCode, string dbType, string dbConnection, Action<ConnectionConfig>? configConnection = null, Action<ISqlSugarClient>? configSugar = null)
|
||||
{
|
||||
ConnectionString = dbConnection,
|
||||
DbType = dbType.Adapt<DbType>(),
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigId = dbCode,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true, // 自动清理缓存
|
||||
IsAutoToUpper = false,
|
||||
PgSqlIsAutoToLower = false,
|
||||
DisableNvarchar = true
|
||||
},
|
||||
};
|
||||
configConnection?.Invoke(config);
|
||||
var sugar = new SqlSugarScope(config, configSugar == null ? ConfigSugar : configSugar);
|
||||
return sugar;
|
||||
}
|
||||
var config = new ConnectionConfig
|
||||
{
|
||||
ConnectionString = dbConnection,
|
||||
DbType = dbType.Adapt<DbType>(),
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigId = dbCode,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true, // 自动清理缓存
|
||||
IsAutoToUpper = false,
|
||||
PgSqlIsAutoToLower = false,
|
||||
DisableNvarchar = true
|
||||
},
|
||||
};
|
||||
configConnection?.Invoke(config);
|
||||
var sugar = new SqlSugarScope(config, configSugar == null ? ConfigSugar : configSugar);
|
||||
return sugar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar默认配置
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public static void ConfigSugar(ISqlSugarClient db)
|
||||
{
|
||||
// 设置超时时间
|
||||
db.Ado.CommandTimeOut = 30;
|
||||
db.Aop.OnLogExecuted = (sql, pars) =>
|
||||
/// <summary>
|
||||
/// SqlSugar默认配置
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public static void ConfigSugar(ISqlSugarClient db)
|
||||
{
|
||||
var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
|
||||
if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
||||
{
|
||||
Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
||||
}
|
||||
else
|
||||
{
|
||||
var oldColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
||||
Console.WriteLine(finalSql);
|
||||
Console.ForegroundColor = oldColor;
|
||||
Console.WriteLine();
|
||||
}
|
||||
};
|
||||
db.Aop.OnError = (ex) =>
|
||||
{
|
||||
Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||
};
|
||||
}
|
||||
// 设置超时时间
|
||||
db.Ado.CommandTimeOut = 30;
|
||||
db.Aop.OnLogExecuted = (sql, pars) =>
|
||||
{
|
||||
var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
|
||||
if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
||||
{
|
||||
Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
||||
}
|
||||
else
|
||||
{
|
||||
var oldColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
||||
Console.WriteLine(finalSql);
|
||||
Console.ForegroundColor = oldColor;
|
||||
Console.WriteLine();
|
||||
}
|
||||
};
|
||||
db.Aop.OnError = (ex) =>
|
||||
{
|
||||
Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||
};
|
||||
}
|
||||
|
||||
public static void CustomSnowId()
|
||||
{
|
||||
StaticConfig.CustomSnowFlakeFunc = () =>
|
||||
public static void CustomSnowId()
|
||||
{
|
||||
return YitIdHelper.NextId();
|
||||
};
|
||||
}
|
||||
}
|
||||
StaticConfig.CustomSnowFlakeFunc = () =>
|
||||
{
|
||||
return YitIdHelper.NextId();
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user