You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.0 KiB
C#

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<IRedisService>();
_codeVesselService = _serviceProvider.GetRequiredService<ICodeVesselService>();
}
/// <summary>
/// 获取缓存数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="cacheKey">缓存key枚举</param>
/// <returns>返回列表</returns>
public async Task<DataResult<List<T>>> GetAllCommonCodeFromCache<T>(SysCacheKeyEnum cacheKey)
{
var currVal =_redisService.GetValue<List<T>>(cacheKey.ToString());
if (currVal == null)
return DataResult<List<T>>.FailedData(new List<T>());
return DataResult<List<T>>.Success(currVal);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheObj">被缓存的详情</param>
/// <param name="cacheKey">缓存key枚举</param>
/// <param name="outSecond">超期时间(毫米)</param>
/// <returns></returns>
public async Task<DataResult<string>> 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<string>.Success(string.Empty);
}
/// <summary>
/// 加载缓存
/// </summary>
/// <param name="cacheObj">被缓存的详情</param>
/// <param name="cacheKey">缓存key枚举</param>
/// <param name="isReload">是否强制刷新缓存</param>
/// <returns></returns>
public async Task<DataResult<string>> 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<string>.Success(string.Empty);
}
}
public enum SysCacheKeyEnum
{
/// <summary>
/// 船名基础信息
/// </summary>
[Description("船名基础信息")]
CommonCodeVessel,
/// <summary>
/// 港口基础信息
/// </summary>
[Description("港口基础信息")]
CommonCodePort,
/// <summary>
/// 包装基础信息
/// </summary>
[Description("包装基础信息")]
CommonCodePackage,
/// <summary>
/// 运输条款基础信息
/// </summary>
[Description("运输条款基础信息")]
CommonCodeService,
/// <summary>
/// 集装箱型基础信息
/// </summary>
[Description("集装箱型基础信息")]
CommonCodeCtn,
/// <summary>
/// 付费方式基础信息
/// </summary>
[Description("付费方式基础信息")]
CommonCodeFrt,
/// <summary>
/// 国家基础信息
/// </summary>
[Description("国家基础信息")]
CommonCodeCountry,
/// <summary>
/// 场站映射信息
/// </summary>
[Description("场站映射信息")]
CommonMappingYard,
/// <summary>
/// 集装箱型映射信息
/// </summary>
[Description("集装箱型映射信息")]
CommonMappingCtn,
/// <summary>
/// 船公司映射信息
/// </summary>
[Description("船公司映射信息")]
CommonMappingCarrier,
/// <summary>
/// 付费方式映射
/// </summary>
[Description("付费方式映射")]
CommonMappingFrt,
/// <summary>
/// 船名映射
/// </summary>
[Description("船名映射")]
CommonMappingVessel,
/// <summary>
/// 船代映射
/// </summary>
[Description("船代映射")]
CommonMappingForwarder
}
}