调度中心嵌入cms
This commit is contained in:
@@ -6,9 +6,10 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func newRedisClient(ctx context.Context, addr, password string) (*redis.Client, error) {
|
||||
func newRedisClient(ctx context.Context, addr, username, password string) (*redis.Client, error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@@ -35,7 +36,9 @@ type Params struct {
|
||||
|
||||
type RedisConfig struct {
|
||||
Addr string
|
||||
Username string
|
||||
Password string
|
||||
Client *redis.Client
|
||||
}
|
||||
|
||||
type Summary struct {
|
||||
@@ -216,11 +219,19 @@ func invalidateBatchCache(ctx context.Context, db *mongo.Database, redisCfg Redi
|
||||
return nil
|
||||
}
|
||||
|
||||
rdb, err := newRedisClient(ctx, redisCfg.Addr, redisCfg.Password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect redis: %w", err)
|
||||
rdb := redisCfg.Client
|
||||
shouldClose := false
|
||||
if rdb == nil {
|
||||
var err error
|
||||
rdb, err = newRedisClient(ctx, redisCfg.Addr, redisCfg.Username, redisCfg.Password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect redis: %w", err)
|
||||
}
|
||||
shouldClose = true
|
||||
}
|
||||
if shouldClose {
|
||||
defer rdb.Close()
|
||||
}
|
||||
defer rdb.Close()
|
||||
|
||||
if err := rdb.Del(ctx, keys...).Err(); err != nil {
|
||||
return fmt.Errorf("redis del: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user