|
|
@ -28,7 +28,14 @@ type DataBases struct {
|
|
|
|
mgoSession *mgo.Session
|
|
|
|
mgoSession *mgo.Session
|
|
|
|
//redisPool *redis.Pool
|
|
|
|
//redisPool *redis.Pool
|
|
|
|
mongoClient *mongo.Client
|
|
|
|
mongoClient *mongo.Client
|
|
|
|
rdb *go_redis.Client
|
|
|
|
rdb go_redis.UniversalClient
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type RedisClient struct {
|
|
|
|
|
|
|
|
client *go_redis.Client
|
|
|
|
|
|
|
|
cluster *go_redis.ClusterClient
|
|
|
|
|
|
|
|
go_redis.UniversalClient
|
|
|
|
|
|
|
|
enableCluster bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func key(dbAddress, dbName string) string {
|
|
|
|
func key(dbAddress, dbName string) string {
|
|
|
@ -112,24 +119,30 @@ func init() {
|
|
|
|
// )
|
|
|
|
// )
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if config.Config.Redis.EnableCluster {
|
|
|
|
|
|
|
|
DB.rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{
|
|
|
|
|
|
|
|
Addrs: []string{config.Config.Redis.DBAddress},
|
|
|
|
|
|
|
|
PoolSize: 50,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = DB.rdb.Ping(ctx).Result()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DB.rdb = go_redis.NewClient(&go_redis.Options{
|
|
|
|
DB.rdb = go_redis.NewClient(&go_redis.Options{
|
|
|
|
Addr: config.Config.Redis.DBAddress,
|
|
|
|
Addr: config.Config.Redis.DBAddress,
|
|
|
|
Password: config.Config.Redis.DBPassWord, // no password set
|
|
|
|
Password: config.Config.Redis.DBPassWord, // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
DB: 0, // use default DB
|
|
|
|
PoolSize: 100, // 连接池大小
|
|
|
|
PoolSize: 100, // 连接池大小
|
|
|
|
})
|
|
|
|
})
|
|
|
|
//DB.rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{
|
|
|
|
|
|
|
|
// Addrs: []string{config.Config.Redis.DBAddress},
|
|
|
|
|
|
|
|
// PoolSize: 50,
|
|
|
|
|
|
|
|
//Password: config.Config.Redis.DBPassWord,
|
|
|
|
|
|
|
|
//})
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err = DB.rdb.Ping(ctx).Result()
|
|
|
|
_, err = DB.rdb.Ping(ctx).Result()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func createMongoIndex(client *mongo.Client, collection string, isUnique bool, keys ...string) error {
|
|
|
|
func createMongoIndex(client *mongo.Client, collection string, isUnique bool, keys ...string) error {
|
|
|
|
db := client.Database(config.Config.Mongo.DBDatabase).Collection(collection)
|
|
|
|
db := client.Database(config.Config.Mongo.DBDatabase).Collection(collection)
|
|
|
|