Skip to content

Commit 8953de4

Browse files
author
zicorn
committed
[update] fix conflict
1 parent f5acef0 commit 8953de4

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ REDIS_SENTINEL_USERNAME=
7474
REDIS_SENTINEL_PASSWORD=
7575
REDIS_SENTINEL_SOCKET_TIMEOUT=0.1
7676

77+
# redis cluster
78+
# Cluster Format: `<ip1>:<port1>,<ip2:<port2>`
79+
REDIS_USE_CLUSTERS=false
80+
REDIS_CLUSTERS=
81+
REDIS_CLUSTERS_PASSWORD=
82+
7783
DB_USERNAME=postgres
7884
DB_PASSWORD=difyai123456
7985
DB_HOST=localhost

internal/utils/cache/redis.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func InitRedisClusterClient(addrs []string, password string, useSsl bool) error
8383
opts.TLSConfig = &tls.Config{}
8484
}
8585

86-
client := redis.NewClusterClient(opts)
86+
client = redis.NewClusterClient(opts)
8787

8888
if _, err := client.Ping(context.Background()).Result(); err != nil {
8989
return err
@@ -526,8 +526,15 @@ func Publish(channel string, message any, context ...redis.Cmdable) error {
526526
}
527527

528528
func Subscribe[T any](channel string) (<-chan T, func()) {
529-
pubsub := client.Subscribe(ctx, channel)
530529
ch := make(chan T)
530+
531+
if client == nil {
532+
log.Error("redis client not initialized")
533+
close(ch)
534+
return ch, func() {}
535+
}
536+
537+
pubsub := client.Subscribe(ctx, channel)
531538
connectionEstablished := make(chan bool)
532539

533540
go func() {

internal/utils/cache/redis_auto_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// Set the value with key
1212
func AutoSet[T any](key string, value T, context ...redis.Cmdable) error {
13-
if redisClient == nil {
13+
if client == nil {
1414
return ErrDBNotInit
1515
}
1616

@@ -32,7 +32,7 @@ func AutoGet[T any](key string, context ...redis.Cmdable) (*T, error) {
3232

3333
// Get the value with key, fallback to getter if not found, and set the value to cache
3434
func AutoGetWithGetter[T any](key string, getter func() (*T, error), context ...redis.Cmdable) (*T, error) {
35-
if redisClient == nil {
35+
if client == nil {
3636
return nil, ErrDBNotInit
3737
}
3838

@@ -66,7 +66,7 @@ func AutoGetWithGetter[T any](key string, getter func() (*T, error), context ...
6666

6767
// Delete the value with key
6868
func AutoDelete[T any](key string, context ...redis.Cmdable) (int64, error) {
69-
if redisClient == nil {
69+
if client == nil {
7070
return 0, ErrDBNotInit
7171
}
7272

0 commit comments

Comments
 (0)