using SJZY.Expand.ABP.Core;
using SJZY.Track.Domain.Aggregates.TrackStatusAggregate;
using SJZY.Track.Domain.Extensions;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Caching;
///
/// 轨迹状态id名称字典缓存
///
public class TrackStatusNameDictCache : BasicCacheForPeriodicRefresh, ISingletonDependency
{
private readonly ITrackStatusRepository _trackStatusRepository;
public TrackStatusNameDictCache(ITrackStatusRepository trackStatusRepository) : base(RedisHelper.Client)
{
_trackStatusRepository = trackStatusRepository;
}
protected override string BuildRedisKey(int key) => $"{GlobalConst.TrackDataDictKeyPrefix}:TrackStatus:{key}";
protected override TimeSpan RedisExpire => TimeSpan.FromSeconds(-1); // 设置缓存不过期,指定id状态删除时仍能提供兜底状态名称
protected override TimeSpan LocalExpire => TimeSpan.FromMinutes(5); // 本地缓存5分钟过期后在次请求会刷新数据
protected override TimeSpan MissingKeySetNullRedisExpire => TimeSpan.FromMinutes(1); // 缓存不存在时设置redisNull
protected override async ValueTask LoadAsync(int key)
{
if (key == 0) return null;
return await _trackStatusRepository.GetFirstAsync(x => x.Id == key, x => x.Status);
}
protected override async ValueTask> LoadAsync(HashSet keys)
{
if (keys.Count == 0) return Empty.Dict;
var result = await _trackStatusRepository.GetListAsync(x => keys.Contains(x.Id), x => new
{
x.Id,
x.Status
});
return result.Count == 0
? Empty.Dict
: result.ToDictionary(x => x.Id, y => y.Status);
}
}