DeviceTriggerIncludeMinuteCache.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.usky.rule.cache;
  2. import com.usky.common.redis.core.RedisHelper;
  3. import com.usky.rule.util.JsonUtil;
  4. import com.usky.rule.vo.ConditionExpression;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.concurrent.TimeUnit;
  10. /**
  11. * 设备触发“包含分钟”条件缓存,使用 RedisHelper 存储。
  12. * 单条条件过期时间 1 天;按 ruleEngineId 可批量删除。
  13. */
  14. @Component
  15. public class DeviceTriggerIncludeMinuteCache {
  16. private static final String KEY_PREFIX = "ruleEngine:condition:";
  17. private static final String INDEX_KEY_PREFIX = "ruleEngine:keys:";
  18. private static final long EXPIRE_DAYS = 1L;
  19. @Autowired
  20. private RedisHelper redisHelper;
  21. public ConditionExpression getConditions(Long ruleEngineId, String device, String identifier, String expression) {
  22. String key = buildConditionKey(ruleEngineId, device, identifier, expression);
  23. if (!redisHelper.hasKey(key)) {
  24. return null;
  25. }
  26. Object val = redisHelper.get(key);
  27. if (val == null) {
  28. return null;
  29. }
  30. return JsonUtil.toObject(val.toString(), ConditionExpression.class);
  31. }
  32. public void setCondition(Long ruleEngineId, String device, String identifier, ConditionExpression condition) {
  33. String key = buildConditionKey(ruleEngineId, device, identifier, condition.getExpression());
  34. redisHelper.set(key, JsonUtil.toJson(condition), EXPIRE_DAYS, TimeUnit.DAYS);
  35. addToIndex(ruleEngineId, key);
  36. }
  37. public void removeCondition(Long ruleEngineId, String device, String identifier, String expression) {
  38. String key = buildConditionKey(ruleEngineId, device, identifier, expression);
  39. redisHelper.delete(key);
  40. removeFromIndex(ruleEngineId, key);
  41. }
  42. public void removeConditions(Long ruleEngineId, String device, String identifier, List<String> expressionList) {
  43. if (expressionList == null || expressionList.isEmpty()) {
  44. return;
  45. }
  46. for (String expression : expressionList) {
  47. removeCondition(ruleEngineId, device, identifier, expression);
  48. }
  49. }
  50. public void deleteConditions(Long ruleEngineId) {
  51. String indexKey = INDEX_KEY_PREFIX + ruleEngineId;
  52. if (!redisHelper.hasKey(indexKey)) {
  53. return;
  54. }
  55. Object val = redisHelper.get(indexKey);
  56. if (val != null) {
  57. List<String> keys = JsonUtil.parseJsonArray(val.toString(), String.class);
  58. if (keys != null) {
  59. for (String key : keys) {
  60. redisHelper.delete(key);
  61. }
  62. }
  63. }
  64. redisHelper.delete(indexKey);
  65. }
  66. private static String buildConditionKey(Long ruleEngineId, String device, String identifier, String expression) {
  67. return KEY_PREFIX + ruleEngineId + ":" + device + ":" + identifier + ":" + expression;
  68. }
  69. private void addToIndex(Long ruleEngineId, String conditionKey) {
  70. String indexKey = INDEX_KEY_PREFIX + ruleEngineId;
  71. List<String> keys = getIndexKeys(indexKey);
  72. if (!keys.contains(conditionKey)) {
  73. keys.add(conditionKey);
  74. redisHelper.set(indexKey, JsonUtil.toJson(keys));
  75. }
  76. }
  77. private void removeFromIndex(Long ruleEngineId, String conditionKey) {
  78. String indexKey = INDEX_KEY_PREFIX + ruleEngineId;
  79. List<String> keys = getIndexKeys(indexKey);
  80. if (keys.remove(conditionKey)) {
  81. if (keys.isEmpty()) {
  82. redisHelper.delete(indexKey);
  83. } else {
  84. redisHelper.set(indexKey, JsonUtil.toJson(keys));
  85. }
  86. }
  87. }
  88. private List<String> getIndexKeys(String indexKey) {
  89. if (!redisHelper.hasKey(indexKey)) {
  90. return new ArrayList<>();
  91. }
  92. Object val = redisHelper.get(indexKey);
  93. if (val == null) {
  94. return new ArrayList<>();
  95. }
  96. List<String> list = JsonUtil.parseJsonArray(val.toString(), String.class);
  97. return list != null ? list : new ArrayList<>();
  98. }
  99. }