添加redis查询功能

This commit is contained in:
junleea 2024-12-21 17:16:38 +08:00
parent db73c16fe3
commit c5258b3fb2
3 changed files with 52 additions and 3 deletions

View File

@ -214,9 +214,14 @@ func GetRedis(c *gin.Context) {
//解析请求参数
var req SetRedisReq
if err := c.ShouldBind(&req); err == nil {
code, message := service.GetToolRedis(req.Key)
req.Value = message
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
if req.Option == "one" {
code, message := service.GetToolRedis(req.Key)
req.Value = message
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
} else if req.Option == "all" {
code, message, data := service.GetAllRedis()
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": data})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return

View File

@ -68,3 +68,11 @@ func GetToolRedis(key string) (code int, message string) {
return proto.SuccessCode, val
}
}
func GetAllRedis() (code int, msg string, data []worker.RedisInfo) {
data, err := worker.GetAllRedisInfo()
if err != nil {
return proto.OperationFailed, err.Error(), nil
}
return proto.SuccessCode, "success", data
}

View File

@ -460,3 +460,39 @@ func GetRedisSetUnion(key1 string, key2 string) []string {
}
return val
}
type RedisInfo struct {
Key string
Value string
Expire time.Duration
}
// 获取所有的key和value,及其对应的过期时间
func GetAllRedisInfo() ([]RedisInfo, error) {
ctx := context.Background()
keys, err := RedisClient.Keys(ctx, "*").Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
var redisInfos []RedisInfo
for _, key := range keys {
val, err := RedisClient.Get(ctx, key).Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
expire, err := RedisClient.TTL(ctx, key).Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
redisInfo := RedisInfo{
Key: key,
Value: val,
Expire: expire,
}
redisInfos = append(redisInfos, redisInfo)
}
return redisInfos, nil
}