Prechádzať zdrojové kódy

首页设备在线状况、实时设备运行、实时警报、实时信息增加缓存

lingpeng.li 5 mesiacov pred
rodič
commit
432cae8e3a

+ 39 - 0
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/homePageData/controller/HomePageDataController.java

@@ -66,7 +66,20 @@ public class HomePageDataController {
     @ApiOperation(value="设备在线情况", notes="设备在线情况")
     @GetMapping(value = "/deviceOnline")
     public Result<Map<String, Object>> deviceOnline() {
+        String cacheKey = "deviceOnline";
+
+        // 尝试从 Redis 获取缓存
+        Map<String, Object> cachedData = (Map<String, Object>) redisTemplate.opsForValue().get(cacheKey);
+        if (cachedData != null) {
+            return Result.OK(cachedData);
+        }
+
+        // 如果缓存中没有数据,则查询数据库
         Map<String, Object> res = homePageDataService.deviceOnline();
+
+        // 存入 Redis,设置 5 分钟过期时间
+        redisTemplate.opsForValue().set(cacheKey, res, 5, TimeUnit.MINUTES);
+
         return Result.OK(res);
     }
 
@@ -80,7 +93,20 @@ public class HomePageDataController {
     @ApiOperation(value="违规计量", notes="违规计量")
     @GetMapping(value = "/illegalMetering")
     public Result<List<Map<String, String>>> illegalMetering() {
+        String cacheKey = "illegalMetering";
+
+        // 尝试从 Redis 获取缓存
+        List<Map<String, String>> cachedData = (List<Map<String, String>>) redisTemplate.opsForValue().get(cacheKey);
+        if (cachedData != null) {
+            return Result.OK(cachedData);
+        }
+
+        // 如果缓存中没有数据,则查询数据库
         List<Map<String, String>> res = homePageDataService.illegalMetering();
+
+        // 存入 Redis,设置 10 分钟过期时间
+        redisTemplate.opsForValue().set(cacheKey, res, 10, TimeUnit.MINUTES);
+
         return Result.OK(res);
     }
 
@@ -165,7 +191,20 @@ public class HomePageDataController {
     @ApiOperation(value="实时报警", notes="实时报警")
     @GetMapping(value = "/realAlarm")
     public Result<List<Map<String, String>>> realAlarm() {
+        String cacheKey = "realAlarm";
+
+        // 尝试从 Redis 获取缓存
+        List<Map<String, String>> cachedData = (List<Map<String, String>>) redisTemplate.opsForValue().get(cacheKey);
+        if (cachedData != null) {
+            return Result.OK(cachedData);
+        }
+
+        // 如果缓存中没有数据,则查询数据库
         List<Map<String, String>> res = homePageDataService.realAlarm();
+
+        // 存入 Redis,设置 5 分钟过期时间
+        redisTemplate.opsForValue().set(cacheKey, res, 5, TimeUnit.MINUTES);
+
         return Result.OK(res);
     }