46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
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;
|
||
|
||
/// <summary>
|
||
/// 轨迹状态id名称字典缓存
|
||
/// </summary>
|
||
public class TrackStatusNameDictCache : BasicCacheForPeriodicRefresh<int, string>, 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<string?> LoadAsync(int key)
|
||
{
|
||
if (key == 0) return null;
|
||
return await _trackStatusRepository.GetFirstAsync(x => x.Id == key, x => x.Status);
|
||
}
|
||
|
||
protected override async ValueTask<Dictionary<int, string>> LoadAsync(HashSet<int> keys)
|
||
{
|
||
if (keys.Count == 0) return Empty<int, string>.Dict;
|
||
var result = await _trackStatusRepository.GetListAsync(x => keys.Contains(x.Id), x => new
|
||
{
|
||
x.Id,
|
||
x.Status
|
||
});
|
||
|
||
return result.Count == 0
|
||
? Empty<int, string>.Dict
|
||
: result.ToDictionary(x => x.Id, y => y.Status);
|
||
}
|
||
}
|