-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis.go
51 lines (41 loc) · 1.21 KB
/
redis.go
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
package distribute
import (
"context"
"encoding/base64"
"os"
log "github.com/towerman1990/homey/logger"
"github.com/go-redis/redis/v9"
"github.com/towerman1990/homey/config"
"go.uber.org/zap"
)
var (
redisClient *redis.Client
WorldChannel string
ForwardChannel string
)
func init() {
WorldChannel = config.Global.Redis.WorldChannel
ForwardChannel = config.Global.Redis.ForwardChannel
redisClient = redis.NewClient(&redis.Options{
Addr: config.Global.Redis.Addr,
Password: config.Global.Redis.Password,
DB: config.Global.Redis.DB,
})
if config.Global.Distribute.Status {
if statusCmd := redisClient.Ping(context.Background()); statusCmd.Err() != nil {
log.Logger.Error("failed to ping redis server", zap.String("error", statusCmd.Err().Error()))
os.Exit(1)
}
}
}
func GetRedisClient() *redis.Client {
return redisClient
}
func PublishWorldMsg(ctx context.Context, data []byte) (err error) {
_, err = redisClient.Publish(ctx, WorldChannel, base64.StdEncoding.EncodeToString(data)).Result()
return
}
func PublishForwardMsg(ctx context.Context, data []byte) (err error) {
_, err = redisClient.Publish(ctx, ForwardChannel, base64.StdEncoding.EncodeToString(data)).Result()
return
}