using DS.Module.Core; using DS.Module.RedisModule; using DS.WMS.Core.Code.Entity; using DS.WMS.Core.Code.Interface; using DS.WMS.Core.Sys.Interface; using LanguageExt; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DS.WMS.Core.Sys.Method { public class SysCacheService : ISysCacheService { private readonly IServiceProvider _serviceProvider; private readonly IRedisService _redisService; private readonly ICodeVesselService _codeVesselService; public SysCacheService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; _redisService = _serviceProvider.GetRequiredService(); _codeVesselService = _serviceProvider.GetRequiredService(); } /// /// 获取缓存数据 /// /// 数据类型 /// 缓存key枚举 /// 返回列表 public async Task>> GetAllCommonCodeFromCache(SysCacheKeyEnum cacheKey) { var currVal =_redisService.GetValue>(cacheKey.ToString()); if (currVal == null) return DataResult>.FailedData(new List()); return DataResult>.Success(currVal); } /// /// 设置缓存 /// /// 被缓存的详情 /// 缓存key枚举 /// 超期时间(毫米) /// public async Task> SetCommonCode(object cacheObj, SysCacheKeyEnum cacheKey, int outSecond = 0) { if (outSecond > 0) _redisService.SetValue(cacheKey.ToString(), JsonConvert.SerializeObject(cacheObj), outSecond); _redisService.SetValue(cacheKey.ToString(), JsonConvert.SerializeObject(cacheObj)); return DataResult.Success(string.Empty); } /// /// 加载缓存 /// /// 被缓存的详情 /// 缓存key枚举 /// 是否强制刷新缓存 /// public async Task> LoadCache(object cacheObj, SysCacheKeyEnum cacheKey, bool isReload = false) { if (isReload) { if (_redisService.Exists(cacheKey.ToString())) _redisService.DeleteKey(cacheKey.ToString()); } _redisService.SetValue(cacheKey.ToString(), JsonConvert.SerializeObject(cacheObj)); return DataResult.Success(string.Empty); } } public enum SysCacheKeyEnum { /// /// 船名基础信息 /// [Description("船名基础信息")] CommonCodeVessel, /// /// 港口基础信息 /// [Description("港口基础信息")] CommonCodePort, /// /// 包装基础信息 /// [Description("包装基础信息")] CommonCodePackage, /// /// 运输条款基础信息 /// [Description("运输条款基础信息")] CommonCodeService, /// /// 集装箱型基础信息 /// [Description("集装箱型基础信息")] CommonCodeCtn, /// /// 付费方式基础信息 /// [Description("付费方式基础信息")] CommonCodeFrt, /// /// 国家基础信息 /// [Description("国家基础信息")] CommonCodeCountry, /// /// 场站映射信息 /// [Description("场站映射信息")] CommonMappingYard, /// /// 集装箱型映射信息 /// [Description("集装箱型映射信息")] CommonMappingCtn, /// /// 船公司映射信息 /// [Description("船公司映射信息")] CommonMappingCarrier, /// /// 付费方式映射 /// [Description("付费方式映射")] CommonMappingFrt, /// /// 船名映射 /// [Description("船名映射")] CommonMappingVessel, /// /// 船代映射 /// [Description("船代映射")] CommonMappingForwarder } }