添加redis查询功能
This commit is contained in:
parent
db73c16fe3
commit
c5258b3fb2
|
|
@ -214,9 +214,14 @@ func GetRedis(c *gin.Context) {
|
||||||
//解析请求参数
|
//解析请求参数
|
||||||
var req SetRedisReq
|
var req SetRedisReq
|
||||||
if err := c.ShouldBind(&req); err == nil {
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
if req.Option == "one" {
|
||||||
code, message := service.GetToolRedis(req.Key)
|
code, message := service.GetToolRedis(req.Key)
|
||||||
req.Value = message
|
req.Value = message
|
||||||
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
|
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 {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -68,3 +68,11 @@ func GetToolRedis(key string) (code int, message string) {
|
||||||
return proto.SuccessCode, val
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -460,3 +460,39 @@ func GetRedisSetUnion(key1 string, key2 string) []string {
|
||||||
}
|
}
|
||||||
return val
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue