File size: 2,801 Bytes
3392510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bae4297
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package config

import (
	"augment2api/pkg/logger"
	"context"
	"os"
	"time"

	"github.com/go-redis/redis/v8"
)

var RDB redis.Cmdable

// InitRedisClient This function is called after init()
func InitRedisClient() (err error) {

	RedisConnString := AppConfig.RedisConnString
	if RedisConnString == "" {
		logger.Log.Debug("REDIS_CONN_STRING not set, Redis is not enabled")
		return nil
	}

	opt, err := redis.ParseURL(RedisConnString)
	if err != nil {
		logger.Log.Fatalln("failed to parse Redis connection string: " + err.Error())
	}
	RDB = redis.NewClient(opt)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	_, err = RDB.Ping(ctx).Result()
	if err != nil {
		logger.Log.Fatalln("Redis ping test failed: " + err.Error())
	}
	return err
}

func ParseRedisOption() *redis.Options {
	opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
	if err != nil {
		logger.Log.Fatalln("failed to parse Redis connection string: " + err.Error())
	}
	return opt
}

func RedisSet(key string, value string, expiration time.Duration) error {
	ctx := context.Background()
	return RDB.Set(ctx, key, value, expiration).Err()
}

func RedisGet(key string) (string, error) {
	ctx := context.Background()
	return RDB.Get(ctx, key).Result()
}

func RedisDel(key string) error {
	ctx := context.Background()
	return RDB.Del(ctx, key).Err()
}

// RedisHSet 设置哈希表字段值
func RedisHSet(key, field, value string) error {
	ctx := context.Background()
	return RDB.HSet(ctx, key, field, value).Err()
}

// RedisHGet 获取哈希表字段值
func RedisHGet(key, field string) (string, error) {
	ctx := context.Background()
	return RDB.HGet(ctx, key, field).Result()
}

// RedisExpire 设置键的过期时间
func RedisExpire(key string, expiration time.Duration) error {
	ctx := context.Background()
	return RDB.Expire(ctx, key, expiration).Err()
}

// RedisKeys 获取匹配指定模式的所有键
func RedisKeys(pattern string) ([]string, error) {
	ctx := context.Background()
	return RDB.Keys(ctx, pattern).Result()
}

// RedisExists 检查键是否存在
func RedisExists(key string) (bool, error) {
	ctx := context.Background()
	result, err := RDB.Exists(ctx, key).Result()
	if err != nil {
		return false, err
	}
	return result > 0, nil
}

// RedisIncr 增加Redis中的计数器
func RedisIncr(key string) error {
	ctx := context.Background()
	_, err := RDB.Incr(ctx, key).Result()
	return err
}

// RedisHExists 检查哈希表字段是否存在
func RedisHExists(key, field string) (bool, error) {
	ctx := context.Background()
	return RDB.HExists(ctx, key, field).Result()
}

// RedisHGetAll 获取哈希表中的所有字段和值
func RedisHGetAll(key string) (map[string]string, error) {
	ctx := context.Background()
	return RDB.HGetAll(ctx, key).Result()
}