From 8c133187b60c9b384f06c8d2a0c5b2063b8fd9c4 Mon Sep 17 00:00:00 2001 From: "nodyang@aliyun.com" Date: Sat, 14 Sep 2024 14:09:28 +0800 Subject: [PATCH] QuListCache --- .../SlnMesnac.Repository.csproj | 1 + .../service/baseCode/BaseCodeServiceImpl.cs | 27 ++++++++++++--- .../service/baseCode/IBaseCodeService.cs | 5 +-- SlnMesnac.TouchSocket/TcpServer.cs | 34 ++++++++++--------- SlnMesnac.WPF/Startup.cs | 2 +- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/SlnMesnac.Repository/SlnMesnac.Repository.csproj b/SlnMesnac.Repository/SlnMesnac.Repository.csproj index b733819..c432d79 100644 --- a/SlnMesnac.Repository/SlnMesnac.Repository.csproj +++ b/SlnMesnac.Repository/SlnMesnac.Repository.csproj @@ -6,6 +6,7 @@ + diff --git a/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs b/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs index b90d0a6..6dde356 100644 --- a/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs +++ b/SlnMesnac.Repository/service/baseCode/BaseCodeServiceImpl.cs @@ -1,4 +1,7 @@ -using System.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Caching.Memory; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service.@base; @@ -6,9 +9,11 @@ namespace SlnMesnac.Repository.service { public class BaseCodeServiceImpl:BaseServiceImpl,IBaseCodeService { - public BaseCodeServiceImpl(Repository rep) : base(rep) + private readonly IMemoryCache _memoryCache; + + public BaseCodeServiceImpl(Repository rep, IMemoryCache memoryCache) : base(rep) { - + _memoryCache = memoryCache; } public void CreateBaseCode() @@ -16,6 +21,20 @@ namespace SlnMesnac.Repository.service _rep.Context.CodeFirst.InitTables(typeof(BaseCode)); } + + public List QuListCache() + { + string key = "QuListCache"; + if (_memoryCache.TryGetValue>(key, out var list)) + { + return list; + } + var entity = Query(); + _memoryCache.Set(key, entity, TimeSpan.FromMinutes(5)); + + return entity; + } + /// /// 根据Code去查询单个数据 /// @@ -23,7 +42,7 @@ namespace SlnMesnac.Repository.service /// public BaseCode QueryCode(string code) { - return this.Query(x => x.Code == code).First(); + return Query(x => x.Code == code).First(); } } } \ No newline at end of file diff --git a/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs b/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs index 5efca06..32ee284 100644 --- a/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs +++ b/SlnMesnac.Repository/service/baseCode/IBaseCodeService.cs @@ -1,4 +1,5 @@ -using SlnMesnac.Model.domain; +using System.Collections.Generic; +using SlnMesnac.Model.domain; using SlnMesnac.Repository.service.@base; namespace SlnMesnac.Repository.service @@ -6,7 +7,7 @@ namespace SlnMesnac.Repository.service public interface IBaseCodeService:IBaseService { void CreateBaseCode(); - + List QuListCache(); BaseCode QueryCode(string code); } } \ No newline at end of file diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index 89e6bfc..7615732 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -3,14 +3,13 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.Repository.service.ScanLog; using TouchSocket.Core; using TouchSocket.Sockets; -using JsonSerializer = System.Text.Json.JsonSerializer; + #region << 版 本 注 释 >> /*-------------------------------------------------------------------- @@ -57,8 +56,8 @@ namespace SlnMesnac.TouchSocket - public Action ScanLogSocketAction; - public Action ScanStatusSocketAction; + public Action? ScanLogSocketAction; + public Action? ScanStatusSocketAction; public TcpServer(ILogger logger,TcpService tcpService, IScanLogService scan, IBaseCodeService baseCodeService) @@ -74,22 +73,25 @@ namespace SlnMesnac.TouchSocket try { _service.Connecting = (client, e) => { - _logger.LogInformation($"客户端{client.IP}正在接入服务"); + _logger.LogInformation("客户端{ClientIp}正在接入服务", client.IP); return EasyTask.CompletedTask; }; _service.Connected = (client, e) => { - _logger.LogInformation($"客户端{client.IP}接入服务成功"); + _logger.LogInformation("客户端{ClientIp}接入服务成功", client.IP); // RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; _service.Disconnected = (client, e) => { - _logger.LogInformation($"客户端{client.IP}断开连接"); + _logger.LogInformation("客户端{ClientIp}断开连接", client.IP); // RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; _service.Received = (client, e) => { - //从客户端收到信息 + + try + { + var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len _logger.LogInformation("收到数据:{Data}",mes); // byte[] receivedBuffer = new byte[e.ByteBlock.Len]; @@ -116,8 +118,7 @@ namespace SlnMesnac.TouchSocket Rfid = scanStatus.rfid, Ocr = scanStatus.ocr, Url = scanStatus.url - - + }); @@ -128,10 +129,10 @@ namespace SlnMesnac.TouchSocket { ScanStatusSocket scanStatus = mes.FromJsonString(); ScanStatusSocketAction?.Invoke(scanStatus); - if (scanStatus.device_status != null && scanStatus.device_status.Count > 0) + if (scanStatus.is_alarm == 1 && scanStatus.device_status != null && scanStatus.device_status.Count > 0) { List ls = new List(); - var baseCodes = _baseCodeService.Query(); + var baseCodes = _baseCodeService.QuListCache(); foreach (var baseCode in scanStatus.device_status) { var enBaseCode = baseCodes.FirstOrDefault(x=>x.Code==baseCode); @@ -144,11 +145,12 @@ namespace SlnMesnac.TouchSocket } } } - - // DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp); - // DateTime dateTime = dateTimeOffset.UtcDateTime; - client.SendAsync(new Back().ToJsonString()); + } + catch (Exception exception) + { + _logger.LogError(exception,exception.Message); + } return EasyTask.CompletedTask; }; diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index 502d594..f097aa8 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -26,7 +26,7 @@ namespace SlnMesnac.WPF public void ConfigureServices(IServiceCollection services) { services.AddControllers(); - + services.AddMemoryCache(); //注册AppConfig services.AddSingleton(provider => {