修改redis查询所有键值

This commit is contained in:
junleea 2024-12-21 18:03:47 +08:00
parent a6ce904059
commit 119420932b
1 changed files with 7 additions and 5 deletions

View File

@ -464,7 +464,8 @@ func GetRedisSetUnion(key1 string, key2 string) []string {
type RedisInfo struct { type RedisInfo struct {
Key string Key string
Value string Value string
Expire time.Duration Type string
Expire int // 过期时间, 单位: 秒
} }
// 获取所有的key和value,及其对应的过期时间 // 获取所有的key和value,及其对应的过期时间
@ -478,7 +479,7 @@ func GetAllRedisInfo() ([]RedisInfo, error) {
var redisInfos []RedisInfo var redisInfos []RedisInfo
for _, key := range keys { for _, key := range keys {
//先查看key类型再根据类型获取value //先查看key类型再根据类型获取value
val, err := getKeyTypeAndData(key) key_type, val, err := getKeyTypeAndData(key)
if err != nil { if err != nil {
fmt.Println("Error getting key: %v", err) fmt.Println("Error getting key: %v", err)
return nil, err return nil, err
@ -491,14 +492,15 @@ func GetAllRedisInfo() ([]RedisInfo, error) {
redisInfo := RedisInfo{ redisInfo := RedisInfo{
Key: key, Key: key,
Value: val, Value: val,
Expire: expire, Type: key_type,
Expire: int(expire.Seconds()),
} }
redisInfos = append(redisInfos, redisInfo) redisInfos = append(redisInfos, redisInfo)
} }
return redisInfos, nil return redisInfos, nil
} }
func getKeyTypeAndData(key string) (string, error) { func getKeyTypeAndData(key string) (string, string, error) {
ctx := context.Background() ctx := context.Background()
key_type := RedisClient.Type(ctx, key).Val() key_type := RedisClient.Type(ctx, key).Val()
var val interface{} var val interface{}
@ -519,5 +521,5 @@ func getKeyTypeAndData(key string) (string, error) {
default: default:
val = "unknown type" val = "unknown type"
} }
return fmt.Sprintf("%v", val), err return key_type, fmt.Sprintf("%v", val), err
} }