refactor: 初始化缓存编译读写表达式

This commit is contained in:
Diego
2024-06-17 13:49:45 +08:00
parent 83bc07c3ff
commit d6b0a9314c
7 changed files with 23 additions and 22 deletions

View File

@@ -28,7 +28,7 @@ public abstract class ReadWriteDevicesSingleStreamDataHandleAdapter<TRequest> :
/// </summary>
/// <param name="ex">异常</param>
/// <param name="error">错误异常</param>
/// <param name="reset">是否调用<see cref="Reset"/></param>
/// <param name="reset">是否调用Reset/></param>
/// <param name="log">是否记录日志</param>
protected override void OnError(Exception ex, string error, bool reset, bool log)
{

View File

@@ -42,7 +42,7 @@ public abstract class ReadWriteDevicesUdpDataHandleAdapter<TRequest> : UdpDataHa
/// </summary>
/// <param name="ex">异常</param>
/// <param name="error">错误异常</param>
/// <param name="reset">是否调用<see cref="Reset"/></param>
/// <param name="reset">是否调用Reset/></param>
/// <param name="log">是否记录日志</param>
protected override void OnError(Exception ex, string error, bool reset, bool log)
{

View File

@@ -14,7 +14,7 @@ using CSScriptLib;
using NewLife.Caching;
namespace ThingsGateway.Foundation;
namespace ThingsGateway.Gateway.Application.Extensions;
/// <summary>
/// 读写表达式脚本
@@ -82,11 +82,7 @@ public static class ExpressionEvaluatorExtension
{
m_waiterLock.Wait();
{
runScript = Instance.Get<ReadWriteExpressions>(field);
if (runScript == null)
{
runScript = AddScript(source);
}
runScript = AddScript(source);
}
}
finally
@@ -102,14 +98,16 @@ public static class ExpressionEvaluatorExtension
internal static ReadWriteExpressions AddScript(string source)
{
var field = $"{CacheKey}-{source}";
if (!source.Contains("return"))
var runScript = Instance.Get<ReadWriteExpressions>(field);
if (runScript == null)
{
source = $"return {source}";//只判断简单脚本中可省略return字符串
}
// 动态加载并执行代码
var runScript = CSScript.Evaluator.With(eval => eval.IsAssemblyUnloadingEnabled = true).LoadCode<ReadWriteExpressions>(
$@"
if (!source.Contains("return"))
{
source = $"return {source}";//只判断简单脚本中可省略return字符串
}
// 动态加载并执行代码
runScript = CSScript.Evaluator.With(eval => eval.IsAssemblyUnloadingEnabled = true).LoadCode<ReadWriteExpressions>(
$@"
using System;
using System.Linq;
using System.Collections.Generic;
@@ -126,8 +124,9 @@ public static class ExpressionEvaluatorExtension
}}
}}
");
GC.Collect();
Instance.Set(field, runScript);
GC.Collect();
Instance.Set(field, runScript);
}
return runScript;
}

View File

@@ -11,6 +11,8 @@
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using ThingsGateway.Gateway.Application.Extensions;
using TouchSocket.Core;
namespace ThingsGateway.Gateway.Application;
@@ -72,14 +74,13 @@ public class CollectDeviceHostedService : DeviceHostedService
var idSet = collectDeviceRunTimes.Where(a => a.RedundantEnable && a.RedundantDeviceId != null).Select(a => a.RedundantDeviceId ?? 0).ToHashSet().ToDictionary(a => a);
var result = collectDeviceRunTimes.Where(a => !idSet.ContainsKey(a.Id));
var scripts = collectDeviceRunTimes.SelectMany(a => a.VariableRunTimes.Where(a=>!a.Value.ReadExpressions.IsNullOrWhiteSpace()).Select(b => b.Value.ReadExpressions)).Concat(collectDeviceRunTimes.SelectMany(a => a.VariableRunTimes.Where(a => !a.Value.WriteExpressions.IsNullOrWhiteSpace()).Select(b => b.Value.WriteExpressions))).Distinct().ToList();
var scripts = collectDeviceRunTimes.SelectMany(a => a.VariableRunTimes.Where(a => !a.Value.ReadExpressions.IsNullOrWhiteSpace()).Select(b => b.Value.ReadExpressions)).Concat(collectDeviceRunTimes.SelectMany(a => a.VariableRunTimes.Where(a => !a.Value.WriteExpressions.IsNullOrWhiteSpace()).Select(b => b.Value.WriteExpressions))).Distinct().ToList();
result.ParallelForEach(collectDeviceRunTime =>
{
if (!_stoppingToken.IsCancellationRequested)
{
try
{
DriverBase driverBase = collectDeviceRunTime.CreateDriver(PluginService);
GetChannelThread(driverBase);
}
@@ -103,7 +104,6 @@ public class CollectDeviceHostedService : DeviceHostedService
}
}
});
}
for (int i = 0; i < 3; i++)
{

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>6.0.3.11</Version>
<Version>6.0.3.12</Version>
</PropertyGroup>
<ItemGroup>

View File

@@ -5,7 +5,7 @@
<PropertyGroup>
<Description>工业设备通讯协议-变量</Description>
<TargetFrameworks>netstandard2.0;net6.0;</TargetFrameworks>
<Version>8.4</Version>
<Version>8.5</Version>
</PropertyGroup>
<ItemGroup>

View File

@@ -13,6 +13,8 @@ using NewLife.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ThingsGateway.Gateway.Application.Extensions;
namespace ThingsGateway.Foundation;
/// <summary>