mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-12-29 00:55:48 +08:00
@@ -1,3 +1,4 @@
|
||||
using NetAdmin.Domain.Dto.Dependency;
|
||||
using NetAdmin.Domain.Dto.Sys.Cache;
|
||||
using NetAdmin.Host.Controllers;
|
||||
using NetAdmin.SysComponent.Application.Modules.Sys;
|
||||
@@ -12,6 +13,14 @@ namespace NetAdmin.SysComponent.Host.Controllers.Sys;
|
||||
[ApiDescriptionSettings(nameof(Sys), Module = nameof(Sys))]
|
||||
public sealed class CacheController(ICacheCache cache) : ControllerBase<ICacheCache, ICacheService>(cache), ICacheModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 批量删除缓存项
|
||||
/// </summary>
|
||||
public Task<int> BulkDeleteEntryAsync(BulkReq<DelEntryReq> req)
|
||||
{
|
||||
return Cache.BulkDeleteEntryAsync(req);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存统计
|
||||
/// </summary>
|
||||
@@ -20,6 +29,14 @@ public sealed class CacheController(ICacheCache cache) : ControllerBase<ICacheCa
|
||||
return Cache.CacheStatisticsAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存项
|
||||
/// </summary>
|
||||
public Task<int> DeleteEntryAsync(DelEntryReq req)
|
||||
{
|
||||
return Cache.DeleteEntryAsync(req);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有缓存项
|
||||
/// </summary>
|
||||
|
||||
@@ -12,6 +12,14 @@ namespace NetAdmin.SysComponent.Host.Controllers.Sys;
|
||||
[ApiDescriptionSettings(nameof(Sys), Module = nameof(Sys))]
|
||||
public sealed class ToolsController(IToolsCache cache) : ControllerBase<IToolsCache, IToolsService>(cache), IToolsModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行SQL语句
|
||||
/// </summary>
|
||||
public Task<object[][]> ExecuteSqlAsync(ExecuteSqlReq req)
|
||||
{
|
||||
return Cache.ExecuteSqlAsync(req);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取更新日志
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Furion.Schedule;
|
||||
using NetAdmin.Host.BackgroundRunning;
|
||||
using NetAdmin.Host.Middlewares;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Jobs;
|
||||
@@ -27,6 +28,11 @@ public sealed class FreeScheduledJob : WorkBase<FreeScheduledJob>, IJob
|
||||
/// <exception cref="NetAdminGetLockerException">加锁失败异常</exception>
|
||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||
{
|
||||
if (SafetyShopHostMiddleware.IsShutdown) {
|
||||
Console.WriteLine(Ln.此节点已下线);
|
||||
return;
|
||||
}
|
||||
|
||||
await WorkflowAsync(true, stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ using NetAdmin.Domain.Dto.Sys.Job;
|
||||
using NetAdmin.Domain.Dto.Sys.JobRecord;
|
||||
using NetAdmin.Host.BackgroundRunning;
|
||||
using NetAdmin.Host.Extensions;
|
||||
using NetAdmin.Host.Middlewares;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Jobs;
|
||||
@@ -42,6 +43,11 @@ public sealed class ScheduledJob : WorkBase<ScheduledJob>, IJob
|
||||
/// <exception cref="NetAdminGetLockerException">加锁失败异常</exception>
|
||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||
{
|
||||
if (SafetyShopHostMiddleware.IsShutdown) {
|
||||
Console.WriteLine(Ln.此节点已下线);
|
||||
return;
|
||||
}
|
||||
|
||||
await WorkflowAsync(stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
#if !DEBUG
|
||||
using System.Collections.Concurrent;
|
||||
using NetAdmin.Domain.Dto.Sys.RequestLog;
|
||||
|
||||
#else
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
#endif
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
@@ -8,6 +14,10 @@ namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
/// </summary>
|
||||
public sealed class OperationLogger : IEventSubscriber
|
||||
{
|
||||
#if !DEBUG
|
||||
private static readonly ConcurrentQueue<CreateRequestLogReq> _requestLogs = new();
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 保存请求日志到数据库
|
||||
/// </summary>
|
||||
@@ -18,7 +28,47 @@ public sealed class OperationLogger : IEventSubscriber
|
||||
return;
|
||||
}
|
||||
|
||||
operationEvent.Data.TruncateStrings();
|
||||
#if DEBUG
|
||||
_ = await App.GetService<IRequestLogService>().CreateAsync(operationEvent.Data).ConfigureAwait(false);
|
||||
#else
|
||||
if (_requestLogs.Count > Numbers.REQUEST_LOG_BUFF_SIZE) {
|
||||
await WriteToDbAsync().ConfigureAwait(false);
|
||||
}
|
||||
else {
|
||||
_requestLogs.Enqueue(operationEvent.Data);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !DEBUG
|
||||
private static async Task WriteToDbAsync()
|
||||
{
|
||||
var inserts = new List<CreateRequestLogReq>(Numbers.REQUEST_LOG_BUFF_SIZE);
|
||||
|
||||
// 批量入库
|
||||
for (var i = 0; i != Numbers.REQUEST_LOG_BUFF_SIZE; ++i) {
|
||||
if (!_requestLogs.TryDequeue(out var log)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
inserts.Add(log);
|
||||
}
|
||||
|
||||
// 如果首尾日期不一致,要分别插入不同的日期分表
|
||||
if (inserts[0].CreatedTime.Date != inserts[^1].CreatedTime.Date) {
|
||||
foreach (var dayInserts in inserts.GroupBy(x => x.CreatedTime.Date)) {
|
||||
await App.GetService<IFreeSql>()
|
||||
.Insert<Sys_RequestLog>(dayInserts.Select(x => x))
|
||||
.ExecuteSqlBulkCopyAsync(tableName: $"{nameof(Sys_RequestLog)}_{dayInserts.Key:yyyyMMdd}")
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
await App.GetService<IFreeSql>()
|
||||
.Insert<Sys_RequestLog>(inserts)
|
||||
.ExecuteSqlBulkCopyAsync(tableName: $"{nameof(Sys_RequestLog)}_{inserts[0].CreatedTime:yyyyMMdd}")
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user