wanshenmean
2026-03-02 bfd4fd8e4a05a681ec10a47992294cf752a764c4
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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WIDESEAWCS_RedisService.Connection;
using WIDESEAWCS_RedisService.Options;
 
namespace WIDESEAWCS_RedisService.Monitoring
{
    public class RedisMonitorService : IRedisMonitorService
    {
        private readonly IRedisConnectionManager _connectionManager;
        private readonly RedisOptions _options;
        private readonly ILogger<RedisMonitorService> _logger;
 
        public RedisMonitorService(
            IRedisConnectionManager connectionManager,
            IOptions<RedisOptions> options,
            ILogger<RedisMonitorService> logger)
        {
            _connectionManager = connectionManager;
            _options = options.Value;
            _logger = logger;
        }
 
        public Dictionary<string, string> GetServerInfo()
        {
            var server = _connectionManager.GetServer();
            var info = server.Info();
            var result = new Dictionary<string, string>();
            foreach (var group in info)
            {
                foreach (var item in group)
                    result[$"{group.Key}:{item.Key}"] = item.Value;
            }
            return result;
        }
 
        public RedisMemoryInfo GetMemoryInfo()
        {
            var server = _connectionManager.GetServer();
            var info = server.Info("memory");
            var memorySection = info.FirstOrDefault();
 
            var memInfo = new RedisMemoryInfo();
            if (memorySection == null) return memInfo;
 
            var dict = memorySection.ToDictionary(x => x.Key, x => x.Value);
            if (dict.TryGetValue("used_memory", out var used))
                memInfo.UsedMemory = long.Parse(used);
            if (dict.TryGetValue("used_memory_human", out var usedH))
                memInfo.UsedMemoryHuman = usedH;
            if (dict.TryGetValue("maxmemory", out var max))
                memInfo.MaxMemory = long.Parse(max);
            if (dict.TryGetValue("maxmemory_human", out var maxH))
                memInfo.MaxMemoryHuman = maxH;
 
            if (memInfo.MaxMemory > 0)
                memInfo.UsagePercent = Math.Round((double)memInfo.UsedMemory / memInfo.MaxMemory * 100, 2);
 
            return memInfo;
        }
 
        public bool HealthCheck()
        {
            try
            {
                var db = _connectionManager.GetDatabase();
                var pong = db.Ping();
                return pong.TotalMilliseconds < 5000;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Redis健康检查失败");
                return false;
            }
        }
 
        public long GetDbSize()
        {
            return _connectionManager.GetServer().DatabaseSize();
        }
 
        public long GetClientCount()
        {
            var info = _connectionManager.GetServer().Info("clients");
            var section = info.FirstOrDefault();
            if (section == null) return 0;
            var dict = section.ToDictionary(x => x.Key, x => x.Value);
            return dict.TryGetValue("connected_clients", out var count) ? long.Parse(count) : 0;
        }
    }
}