houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.controller.admin.redis;
H 2
3 import com.iailab.framework.common.pojo.CommonResult;
4 import com.iailab.module.infra.controller.admin.redis.vo.RedisMonitorRespVO;
5 import com.iailab.module.infra.convert.redis.RedisConvert;
6 import io.swagger.v3.oas.annotations.Operation;
7 import io.swagger.v3.oas.annotations.tags.Tag;
8 import org.springframework.data.redis.connection.RedisServerCommands;
9 import org.springframework.data.redis.core.RedisCallback;
10 import org.springframework.data.redis.core.StringRedisTemplate;
11 import org.springframework.security.access.prepost.PreAuthorize;
12 import org.springframework.web.bind.annotation.GetMapping;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RestController;
15
16 import javax.annotation.Resource;
17 import java.util.Properties;
18
19 import static com.iailab.framework.common.pojo.CommonResult.success;
20
21 @Tag(name = "管理后台 - Redis 监控")
22 @RestController
23 @RequestMapping("/infra/redis")
24 public class RedisController {
25
26     @Resource
27     private StringRedisTemplate stringRedisTemplate;
28
29     @GetMapping("/get-monitor-info")
30     @Operation(summary = "获得 Redis 监控信息")
31     @PreAuthorize("@ss.hasPermission('infra:redis:get-monitor-info')")
32     public CommonResult<RedisMonitorRespVO> getRedisMonitorInfo() {
33         // 获得 Redis 统计信息
34         Properties info = stringRedisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
35         Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize);
36         Properties commandStats = stringRedisTemplate.execute((
37                 RedisCallback<Properties>) connection -> connection.info("commandstats"));
38         assert commandStats != null; // 断言,避免警告
39         // 拼接结果返回
40         return success(RedisConvert.INSTANCE.build(info, dbSize, commandStats));
41     }
42
43 }