Cms.Core/Cms.Core.WebApi/Domain/CacheDomain.cs

52 lines
1.7 KiB
C#

using Cms.Core.WebApi.Config;
using Cms.Core.WebApi.Domain.Impl;
using DG.Core;
using DG.Redis;
using Zxd.Core.Shared.Dto;
namespace Cms.Core.WebApi.Domain
{
internal class CacheDomain : ICacheDomain
{
private readonly IHttpClient _httpClient;
private readonly IRedisManager _redisManager;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _httpContext;
private readonly SystemConfig _systemConfig;
public CacheDomain(IRedisManager redisManager,
IMapper mapper,
IConfiguration configuration,
IHttpContextAccessor httpContext,
IHttpClient httpClient)
{
_redisManager = redisManager;
_mapper = mapper;
_configuration = configuration;
_httpContext = httpContext;
_httpClient = httpClient;
_systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
}
public async Task<List<DeptmentDto>> GetDeptments()
{
var key = $"{CacheKeys.DeptList}";
if (!await _redisManager.ExistsAsync(key))
{
var response = await _httpClient.GetAsync<ApiResult<List<DeptmentDto>>>(_systemConfig.GetDepts());
if (response.Code == 0)
{
await _redisManager.SetAsync(key, response.Data, TimeSpan.FromDays(1));
return response.Data;
}
}
else
{
return await _redisManager.GetListAsync<DeptmentDto>(key);
}
return new List<DeptmentDto>();
}
}
}