QuListCache

master
nodyang@aliyun.com 4 months ago
parent 55c2563e21
commit 8c133187b6

@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

@ -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<BaseCode>,IBaseCodeService
{
public BaseCodeServiceImpl(Repository<BaseCode> rep) : base(rep)
private readonly IMemoryCache _memoryCache;
public BaseCodeServiceImpl(Repository<BaseCode> 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<BaseCode> QuListCache()
{
string key = "QuListCache";
if (_memoryCache.TryGetValue<List<BaseCode>>(key, out var list))
{
return list;
}
var entity = Query();
_memoryCache.Set(key, entity, TimeSpan.FromMinutes(5));
return entity;
}
/// <summary>
/// 根据Code去查询单个数据
/// </summary>
@ -23,7 +42,7 @@ namespace SlnMesnac.Repository.service
/// <returns></returns>
public BaseCode QueryCode(string code)
{
return this.Query(x => x.Code == code).First();
return Query(x => x.Code == code).First();
}
}
}

@ -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<BaseCode>
{
void CreateBaseCode();
List<BaseCode> QuListCache();
BaseCode QueryCode(string code);
}
}

@ -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<ScanLogSocket> ScanLogSocketAction;
public Action<ScanStatusSocket> ScanStatusSocketAction;
public Action<ScanLogSocket>? ScanLogSocketAction;
public Action<ScanStatusSocket>? ScanStatusSocketAction;
public TcpServer(ILogger<TcpServer> 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<ScanStatusSocket>();
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<string> ls = new List<string>();
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;
};

@ -26,7 +26,7 @@ namespace SlnMesnac.WPF
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMemoryCache();
//注册AppConfig
services.AddSingleton<AppConfig>(provider =>
{

Loading…
Cancel
Save