From 0bfc23ac4c25554c3b1e933dc21299b98af1f06f Mon Sep 17 00:00:00 2001 From: qianjiawei <1184704771@qq.com> Date: Fri, 3 Nov 2023 09:53:58 +0800 Subject: [PATCH] =?UTF-8?q?redis=E8=8E=B7=E5=8F=96=E6=95=B0=E9=87=87?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IRedisDataService.cs | 19 ++++++++ .../Tnb.ProductionMgr/RedisDataService.cs | 47 +++++++++++++++++++ common/Tnb.Common/Cache/RedisCache.cs | 8 ++++ 3 files changed, 74 insertions(+) create mode 100644 ProductionMgr/Tnb.ProductionMgr.Interfaces/IRedisDataService.cs create mode 100644 ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs diff --git a/ProductionMgr/Tnb.ProductionMgr.Interfaces/IRedisDataService.cs b/ProductionMgr/Tnb.ProductionMgr.Interfaces/IRedisDataService.cs new file mode 100644 index 00000000..c32ab2af --- /dev/null +++ b/ProductionMgr/Tnb.ProductionMgr.Interfaces/IRedisDataService.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace Tnb.ProductionMgr.Interfaces +{ + public interface IRedisDataService + { + /// + /// 根据机号获取重量 + /// + Task GetWeight(string key, string field); + + + } +} diff --git a/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs b/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs new file mode 100644 index 00000000..58ecd15b --- /dev/null +++ b/ProductionMgr/Tnb.ProductionMgr/RedisDataService.cs @@ -0,0 +1,47 @@ +using System.Collections; +using JNPF.Common.Cache; +using JNPF.Common.Manager; +using JNPF.DependencyInjection; +using JNPF.DynamicApiController; +using JNPF.FriendlyException; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Tnb.ProductionMgr.Interfaces; + +namespace Tnb.ProductionMgr +{ + /// + /// 数据采集 + /// + [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] + [Route("api/[area]/[controller]/[action]")] + public class RedisDataService: IRedisDataService, IDynamicApiController, ITransient + { + private readonly RedisCache _redisCache; + public RedisDataService(RedisCache redisCache) + { + _redisCache = redisCache; + } + /// + /// 根据机号获取重量 + /// + [HttpPost] + public async Task GetWeight(string device, string jihao) + { + decimal result = 0; + if (!_redisCache.HashExist(device, jihao)) + throw Oops.Bah("没有找到" + device + "----" + jihao + "的数据"); + var data = _redisCache.GetHash(device, jihao); + + var res = JsonConvert.DeserializeObject(data); + if (res != null && res["Value"] != null) + { + result=decimal.Parse( res["Value"]!.ToString()); + } + + return data; + } + + } +} diff --git a/common/Tnb.Common/Cache/RedisCache.cs b/common/Tnb.Common/Cache/RedisCache.cs index 5cebbd3d..a58db000 100644 --- a/common/Tnb.Common/Cache/RedisCache.cs +++ b/common/Tnb.Common/Cache/RedisCache.cs @@ -224,4 +224,12 @@ public class RedisCache : ICache, ISingleton long second = RedisHelper.PTtl(key); return DateTime.Now.AddMilliseconds(second); } + public string GetHash(string key, string field) + { + return RedisHelper.HGet(key, field); + } + public bool HashExist(string key, string field) + { + return RedisHelper.HExists(key, field); + } } \ No newline at end of file