track/src/SJZY.Track.Application/Caching/TrackStatusNameDictCache.cs

46 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}