ningshuxia
2022-12-01 ad494f13d2ddf31f142cf7fb908b3a6e90395a1a
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.RedisCache
{
    /// <summary>
    /// 监测记录缓存辅助类
    /// </summary>
    public class MonitorRecordCacheHelper
    {
 
        //Redis客户端辅助类对象
        private readonly RedisClientHelper _redisClient = new RedisClientHelper();
 
        private static string CreateRedisKey(long monitorPointId) 
        {
            return $"linux_test:{monitorPointId}";
        }
 
        //最后一条记录
        private const string _lastRecord = "last-record";
 
 
        #region 最后一条记录
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public void SetLastRecord(Model.MonitorRecord model, out bool redis)
        {
            redis = false;
            if (model == null)
                return;
            var redisKey = CreateRedisKey(model.MonitorPointID);
            redis = _redisClient.HashSetJosn(redisKey, _lastRecord, model);
 
        }
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public void SetLastRecord(IEnumerable<Model.MonitorRecord> list)
        {
            if (list == null || list.Count() < 1)
                return;
            foreach (var item in list)
            {
                SetLastRecord(item,out bool redis);
            }
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public Model.MonitorRecord GetLastRecord(long monitorPointId) 
        {
            var redisKey = CreateRedisKey(monitorPointId);
            return _redisClient.HashGetJson<Model.MonitorRecord>(redisKey, _lastRecord);
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public List<Model.MonitorRecord> GetLastRecord(IEnumerable<long> monitorPointIds)
        {
            if (monitorPointIds == null || monitorPointIds.Count() < 1)
                return default;
            return monitorPointIds.Select(x => GetLastRecord(x)).Where(x => x != null).ToList();
        }
 
 
        #endregion
 
 
 
    }
}