liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.dingzhuo.compute.engine.utils;
 
import com.dingzhuo.compute.engine.message.alarm.AlarmStatus;
import com.dingzhuo.compute.engine.message.device.DeviceStatus;
import com.dingzhuo.energy.common.utils.StringUtils;
import com.dingzhuo.energy.common.utils.time.TimeType;
import com.dingzhuo.energy.data.model.domain.IndexStorage;
import com.dingzhuo.energy.data.model.domain.LimitType;
import com.dingzhuo.energy.data.monitoring.alarm.domain.AlarmItem;
import com.dingzhuo.energy.data.monitoring.device.domain.DeviceFormula;
import com.dingzhuo.energy.dataservice.domain.TagValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class CacheService {
 
  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  private ConcurrentHashMap<String, IndexStorage> indexStorageCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, List<String>> postIndexIdsCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, AlarmItem> alarmItemCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, AlarmStatus> alarmCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, LimitType> limitTypeCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, TagValue> tagValueCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, DeviceFormula> deviceFormulaCache = new ConcurrentHashMap<>();
  private ConcurrentHashMap<String, DeviceStatus> deviceStatusCache = new ConcurrentHashMap<>();
 
  private ConcurrentHashMap<TimeType, Set<String>> registers = new ConcurrentHashMap<>();
 
  /**
   * 缓存指标存储信息.
   *
   * @param indexStorage 指标存储
   */
  public void cacheIndexStorage(IndexStorage indexStorage) {
    String actorId = ActorUtil.buildActorId(indexStorage);
    indexStorageCache.put(actorId, indexStorage);
  }
 
  /**
   * 根据 actorId 获取指标存储.
   *
   * @param actorId
   * @return
   */
  public IndexStorage getIndexStorageCache(String actorId) {
    return indexStorageCache.getOrDefault(actorId, null);
  }
 
  /**
   * 缓存计算指标的后置指标.
   *
   * @param actorId
   * @param postId
   */
  public void cachePostIndex(String actorId, String postId) {
    if (!postIndexIdsCache.containsKey(actorId)) {
      postIndexIdsCache.put(actorId, new ArrayList<>());
    }
 
    postIndexIdsCache.get(actorId).add(postId);
  }
 
  /**
   * 移除后置指标.
   *
   * @param actorId
   * @param postActorId
   */
  public void removePostIndexCache(String actorId, String postActorId) {
    if (!postIndexIdsCache.containsKey(actorId)) {
      postIndexIdsCache.put(actorId, new ArrayList<>());
    }
 
    postIndexIdsCache.get(actorId).remove(postActorId);
  }
 
  /**
   * 获取后置指标actorId.
   *
   * @param actorId
   * @return
   */
  public List<String> getPostActorIds(String actorId) {
    return postIndexIdsCache.getOrDefault(actorId, new ArrayList<>());
  }
 
  public void cacheAlarmItem(AlarmItem alarmItem) {
    String actorId = ActorUtil.buildAlarmActorId(alarmItem);
    alarmItemCache.put(actorId, alarmItem);
  }
 
  public void removeAlarmCache(String actorId) {
    alarmItemCache.remove(actorId);
  }
 
  public void removeIndexCache(String actorId) {
    indexStorageCache.remove(actorId);
  }
 
  public AlarmItem getAlarmItem(String actorId) {
    return alarmItemCache.getOrDefault(actorId, null);
  }
 
  public void cacheAlarmStatus(String indexId, String timeType, String limitType,
      AlarmStatus status) {
    String statusId = ActorUtil.buildAlarmStatusId(indexId, timeType, limitType);
    alarmCache.put(statusId, status);
  }
 
  public void cacheAlarmStatus(String statusId, AlarmStatus status) {
    alarmCache.put(statusId, status);
  }
 
  public AlarmStatus getAlarmStatus(String indexId, String timeType, String limitType) {
    String statusId = ActorUtil.buildAlarmStatusId(indexId, timeType, limitType);
    return alarmCache.getOrDefault(statusId, null);
  }
 
  public void cacheTagValues(List<TagValue> tagValues) {
    tagValues.forEach(tagValue -> tagValueCache.put(tagValue.getTagCode(), tagValue));
  }
 
  public void cacheLimitType(String limitCode, LimitType type) {
    limitTypeCache.put(limitCode, type);
  }
 
  public LimitType getLimitType(String limitType) {
    return limitTypeCache.getOrDefault(limitType, null);
  }
 
  public void cacheTagValue(TagValue tagValue) {
    if (tagValue == null) {
      return;
    }
 
    tagValueCache.put(tagValue.getTagCode(), tagValue);
  }
 
  public TagValue getTagValue(String indexCode) {
    if (StringUtils.isBlank(indexCode)) {
      return null;
    }
 
    return tagValueCache.getOrDefault(indexCode, null);
  }
 
  public void cacheDeviceStatusSetting(DeviceFormula deviceFormula) {
    if (deviceFormula != null) {
      String actorId = ActorUtil.buildActorId(deviceFormula);
      deviceFormulaCache.put(actorId, deviceFormula);
    }
  }
 
  public void removeDeviceStatusSetting(String actorId) {
    deviceFormulaCache.remove(actorId);
  }
 
  public DeviceFormula getDeviceFormula(String actorId) {
    return deviceFormulaCache.getOrDefault(actorId, null);
  }
 
  public DeviceStatus getDeviceStatus(String actorId) {
    return deviceStatusCache.getOrDefault(actorId, null);
  }
 
  public void cacheDeviceStatus(String actorId, DeviceStatus lastStatus) {
    deviceStatusCache.put(actorId, lastStatus);
  }
 
  public ConcurrentHashMap<TimeType, Set<String>> getRegisters() {
    return registers;
  }
}