tangxu
2024-01-09 ddc2780231ea76be74fadb7486401a3d0d17b101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace Yw.RedisCache
{
    /// <summary>
    /// 指标健康评价记录缓存辅助类
    /// </summary>
    public class HealthQuotaEvaluationRecordCacheHelper
    {
        //健康结果记录
        private const string _flag = "health-quota-evaluation-record";
 
        //最后一条记录
        private const string _lastRecord = "last-record";
 
        //Redis客户端辅助类对象
        private readonly RedisClientHelper _redisClient = new();
 
        //获取 RedisKey
        private static string CreateRedisKey(long SignalID)
        {
            return $"{RedisKeyHelper.CreateKey(_flag)}:{SignalID}";
        }
 
 
        #region 记录
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public bool SetLastRecord(Model.HealthQuotaEvaluationRecord model)
        {
            if (model == null)
                return false;
            string redisKey = CreateRedisKey(model.SignalID);
            var hashKey = _lastRecord;
            _redisClient.HashSetJosn(redisKey, hashKey, model);
            return true;
        }
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public bool SetLastRecord(IEnumerable<Model.HealthQuotaEvaluationRecord> list)
        {
            if (list == null || !list.Any())
                return false;
            foreach (var item in list)
            {
                var bol = SetLastRecord(item);
                if (!bol)
                    return false;
            }
            return true;
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public Model.HealthQuotaEvaluationRecord GetLastRecord(long SignalID)
        {
            var redisKey = CreateRedisKey(SignalID);
            var hashKey = _lastRecord;
            return _redisClient.HashGetJson<Model.HealthQuotaEvaluationRecord>(redisKey, hashKey);
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public List<Model.HealthQuotaEvaluationRecord> GetLastRecord(IEnumerable<long> SignalIds)
        {
            if (SignalIds == null || !SignalIds.Any())
                return default;
            return SignalIds.ToList().Select(x => GetLastRecord(x)).Where(x => x != null).ToList();
        }
 
 
        /// <summary>
        /// 删除最后一条记录
        /// </summary>
        public void DeleteLastRecord(long SignalID)
        {
            var redisKey = CreateRedisKey(SignalID);
            var hasKey = _lastRecord;
            _redisClient.HashDelete(redisKey, hasKey);
        }
 
        #endregion
    }
}