签入脚本
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
"easygoadmin/api/v1"
|
||||
"easygoadmin/internal/service"
|
||||
"easygoadmin/utility/cache"
|
||||
)
|
||||
|
||||
// ============================== 缓存监控业务实现 ==============================
|
||||
|
||||
// sCacheMonitor 缓存监控业务实现
|
||||
// Redis 模式解析 INFO 各节;内存降级模式仅返回模式标识
|
||||
type sCacheMonitor struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterCacheMonitor(&sCacheMonitor{})
|
||||
}
|
||||
|
||||
// Info 查询缓存监控信息
|
||||
func (s *sCacheMonitor) Info(ctx context.Context, req *v1.CacheInfoReq) (res *v1.CacheInfoRes, err error) {
|
||||
if cache.UsingMemory() {
|
||||
return &v1.CacheInfoRes{Mode: "memory", Connected: false}, nil
|
||||
}
|
||||
res = &v1.CacheInfoRes{Mode: "redis", Connected: true}
|
||||
sections := map[string]map[string]string{}
|
||||
for _, name := range []string{"server", "memory", "stats", "clients", "keyspace"} {
|
||||
v, err := g.Redis().Do(ctx, "INFO", name)
|
||||
if err != nil {
|
||||
res.Connected = false
|
||||
return res, nil
|
||||
}
|
||||
sections[name] = parseRedisInfo(v.String())
|
||||
}
|
||||
|
||||
res.Version = sections["server"]["redis_version"]
|
||||
if sec, e := strconv.ParseInt(sections["server"]["uptime_in_seconds"], 10, 64); e == nil {
|
||||
res.Uptime = (time.Duration(sec) * time.Second).String()
|
||||
}
|
||||
res.UsedMemory = sections["memory"]["used_memory_human"]
|
||||
res.UsedMemoryPeak = sections["memory"]["used_memory_peak_human"]
|
||||
res.MaxMemory = sections["memory"]["maxmemory_human"]
|
||||
res.FragRatio = sections["memory"]["mem_fragmentation_ratio"]
|
||||
res.ConnectedClients, _ = strconv.ParseInt(sections["clients"]["connected_clients"], 10, 64)
|
||||
res.ExpiredKeys, _ = strconv.ParseInt(sections["stats"]["expired_keys"], 10, 64)
|
||||
res.EvictedKeys, _ = strconv.ParseInt(sections["stats"]["evicted_keys"], 10, 64)
|
||||
res.TotalCommands, _ = strconv.ParseInt(sections["stats"]["total_commands_processed"], 10, 64)
|
||||
res.TotalConns, _ = strconv.ParseInt(sections["stats"]["total_connections_received"], 10, 64)
|
||||
res.Hits, _ = strconv.ParseInt(sections["stats"]["keyspace_hits"], 10, 64)
|
||||
res.Misses, _ = strconv.ParseInt(sections["stats"]["keyspace_misses"], 10, 64)
|
||||
if total := res.Hits + res.Misses; total > 0 {
|
||||
res.HitRate = strconv.FormatFloat(float64(res.Hits)/float64(total)*100, 'f', 2, 64)
|
||||
} else {
|
||||
res.HitRate = "0.00"
|
||||
}
|
||||
if v, e := g.Redis().Do(ctx, "DBSIZE"); e == nil {
|
||||
res.Keys = v.Int64()
|
||||
}
|
||||
for k, line := range sections["keyspace"] {
|
||||
if !strings.HasPrefix(k, "db") {
|
||||
continue
|
||||
}
|
||||
entry := v1.CacheKeySpaceDb{Db: k}
|
||||
for _, kv := range strings.Split(line, ",") {
|
||||
parts := strings.SplitN(kv, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
switch parts[0] {
|
||||
case "keys":
|
||||
entry.Keys, _ = strconv.ParseInt(parts[1], 10, 64)
|
||||
case "expires":
|
||||
entry.Expires, _ = strconv.ParseInt(parts[1], 10, 64)
|
||||
}
|
||||
}
|
||||
res.KeySpace = append(res.KeySpace, entry)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// parseRedisInfo 解析 INFO 输出为 键→值 映射
|
||||
func parseRedisInfo(s string) map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, line := range strings.Split(s, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
if idx := strings.Index(line, ":"); idx > 0 {
|
||||
out[line[:idx]] = strings.TrimSpace(line[idx+1:])
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user